概述
https://docs.agora.io/cn/rtc/restfulapi/#/
package com.youju.framework.utils;
import com.alibaba.fastjson.JSONObject;
import com.youju.framework.utils.shengwang.RtcTokenBuilder;
import com.youju.framework.utils.shengwang.RtmTokenBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import java.util.Base64;
/**
* 创建声网的功能(语音通话)
*
* @Author LC
* @Date 2021/7/5
*/
@Component
public class ShengWangUtils {
private RestTemplate restTemplate = new RestTemplate();
@Value("${shengwang.appId}")
private String appId;
@Value("${shengwang.appCertificate}")
private String appCertificate;
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getAppCertificate() {
return appCertificate;
}
public void setAppCertificate(String appCertificate) {
this.appCertificate = appCertificate;
}
// private String userId = "2882341273"; // 用于登录 RTM 系统的用户 ID。你需要自行设定。
private int expireTimestamp = 360000000; // 此参数暂不生效,每个 RTM Token 的有效期都是 24 小时
// private String channelName = "7d72365eb983485397e3e3f9d460bdda"; // 暂定
private int expirationTimeInSeconds = 360000000;
/**
* 获得声网的 Rtmtoken - 聊天
* 用户登录时使用已过期的 RTM Token 则会返回 LOGIN_ERR_TOKEN_EXPIRED 错误。
* Agora 推荐你通过定时(例如每小时)更新 Token 来解决 Token 过期问题。
*
* @return token
*/
public String getSWRtmToken(String userId) {
try {
RtmTokenBuilder token = new RtmTokenBuilder();
String result = token.buildToken(appId, appCertificate, userId, RtmTokenBuilder.Role.Rtm_User, expireTimestamp);
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获得声网的 Rtctoken
* 用户登录时使用已过期的 RTM Token 则会返回 LOGIN_ERR_TOKEN_EXPIRED 错误。
* Agora 推荐你通过定时(例如每小时)更新 Token 来解决 Token 过期问题。
*
* @return token
*/
public String getSWRtcToken(String userId, String channelName) {
try {
RtcTokenBuilder token = new RtcTokenBuilder();
int timestamp = (int) (System.currentTimeMillis() / 1000 + expirationTimeInSeconds);
String result = token.buildTokenWithUserAccount(appId, appCertificate,
channelName, userId, RtcTokenBuilder.Role.Role_Publisher, timestamp);
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取httptoken
* 语音通话/视频通话/互动直播/极速直播
*
* @return httptoken
*/
public String getSWHttpToken() {
// 客户 ID
final String customerKey = "04f52f333d9e4c50a3f811ac415c3f71";
// 客户密钥
final String customerSecret = "a82bd3ee77c1425483bc6cafe7e06b57";
// 拼接客户 ID 和客户密钥并使用 base64 编码
String plainCredentials = customerKey + ":" + customerSecret;
String base64Credentials = new String(Base64.getEncoder().encode(plainCredentials.getBytes()));
// 创建 authorization header
String authorizationHeader = "Basic " + base64Credentials;
/* HttpClient client = HttpClient.newHttpClient();
// 创建 HTTP 请求对象
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.agora.io/dev/v1/projects"))
.GET()
.header("Authorization", authorizationHeader)
.header("Content-Type", "application/json")
.build();
// 发送 HTTP 请求
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());*/
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", authorizationHeader);
headers.add("Content-Type", "application/json");
HttpEntity entity = new HttpEntity(headers);
ResponseEntity<String> exchange = restTemplate.exchange("https://api.agora.io/dev/v1/projects",
HttpMethod.GET, entity, String.class);
return exchange.getBody();
}
/**
* 发送消息前的token验证(实时消息采用验证)
*
* @return
*/
public String checkToken(String choosedToken) {
// RTM Token
String tokenHeader = choosedToken;
// 生成 RTM Token 时使用的 user ID
String uidHeader = "test_user"; // 暂定,后面得修改
// 构建请求对象
/* HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.agora.io/dev/v2/project/<Your App ID>/rtm/vendor/user_events"))
.GET()
// 在 header 中添加 x-agora-token 字段
.header("x-agora-token", tokenHeader)
// 在 header 中添加 x-agora-uid 字段
.header("x-agora-uid", uidHeader)
.header("Content-Type", "application/json")
.build();
// 发送请求
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());*/
HttpHeaders headers = new HttpHeaders();
headers.add("x-agora-token", tokenHeader);
headers.add("x-agora-uid", uidHeader);
headers.add("Content-Type", "application/json");
HttpEntity entity = new HttpEntity(headers);
ResponseEntity<String> exchange = restTemplate.exchange("https://api.agora.io/dev/v2/project/" + appId + "/rtm/vendor/user_events",
HttpMethod.GET, entity, String.class);
return exchange.getBody();
}
/**
* 获得所有的项目信息
* 不需要任何参数,直接请求
* GET /dev/v1/projects
*
* @return 所有项目信息(里面包含项目的id)
*/
public JSONObject getAllProjectInfo() {
try {
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", getSWHttpToken());
HttpEntity entity = new HttpEntity(headers);
ResponseEntity<JSONObject> exchange = restTemplate.exchange("https://api.agora.io/dev/v1/projects", HttpMethod.GET, entity, JSONObject.class);
return exchange.getBody();
} catch (RestClientException e) {
e.printStackTrace();
return null;
}
}
/**
* 获取指定项目的信息
* GET /dev/v1/project
* 前端获得所有心昂丶信息,把项目id存储起来
*
* @return 单个项目的信息
*/
public JSONObject getProjectInfo(String projectId, String projectName) {
try {
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", getSWHttpToken());
JSONObject body = new JSONObject();
body.put("id", projectId);
body.put("Name", projectName);
HttpEntity entity = new HttpEntity(body, headers);
ResponseEntity<JSONObject> exchange = restTemplate.exchange("https://api.agora.io/dev/v1/project", HttpMethod.GET, entity, JSONObject.class);
return exchange.getBody();
} catch (RestClientException e) {
e.printStackTrace();
return null;
}
}
/**
* 设置录制服务器的id
* POSt /dev/v1/recording_config
* 让项目信息的"recording_server": 从null变成"10.12.1.5:8080"
*
* @param projectId 项目id
* @param serverId 服务器地址
* @return 返回项目信息
*/
public JSONObject setProjectServerId(String projectId, String serverId) {
try {
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", getSWHttpToken());
JSONObject body = new JSONObject();
body.put("id", projectId);
body.put("recording_server", serverId);
HttpEntity entity = new HttpEntity(body, headers);
ResponseEntity<JSONObject> exchange = restTemplate.exchange("https://api.agora.io/dev/v1/project", HttpMethod.POST, entity, JSONObject.class);
return exchange.getBody();
} catch (RestClientException e) {
e.printStackTrace();
return null;
}
}
/**
* 获得单个项目的数据(使用情况)
* GET /dev/v3/usage
*
* @param projectId 项目id
* @param fromDate 开始时间
* @param toDate 结束时间
* @param business 业务类型 (default音视频, transcodeDuration转码, recording现场录音, cloudRecording云录音, miniapp小程序)
* @return 返回项目使用信息
*/
public JSONObject setProjectServerId(String projectId, String fromDate, String toDate, String business) {
try {
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", getSWHttpToken());
JSONObject body = new JSONObject();
body.put("id", projectId);
body.put("from_date", fromDate);
body.put("to_date", toDate);
body.put("business", business);
HttpEntity entity = new HttpEntity(body, headers);
ResponseEntity<JSONObject> exchange = restTemplate.exchange("https://api.agora.io/dev/v3/usage", HttpMethod.GET, entity, JSONObject.class);
return exchange.getBody();
} catch (RestClientException e) {
e.printStackTrace();
return null;
}
}
/**
* 获得一个频道下面的所有用户的信息
* <p>
* GET /dev/v1/channel/user/{appid}/{channelName}
*
* @param appid appid
* @param channelName 频道名称
* @return 返回所有用户信息
*/
public JSONObject getChannelUserList(String appid, String channelName) {
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", getSWHttpToken());
HttpEntity entity = new HttpEntity(headers);
ResponseEntity<JSONObject> exchange = restTemplate.exchange("https://api.agora.io/dev/v1/channel/user/{appid}/{channelName}",
HttpMethod.GET, entity, JSONObject.class, appid, channelName);
return exchange.getBody();
}
/**
* 一个项目下的频道列表
* <p>
* GET /dev/v1/channel/{appid}
*
* @param appid appid
* @param page_no 频道名称
* @param page_size 频道名称
* @return 返回频道列表
*/
public JSONObject getChannelList(String appid, String page_no, String page_size) {
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", getSWHttpToken());
JSONObject body = new JSONObject();
body.put("appid", appid);
body.put("page_no", page_no);
body.put("page_size", page_size);
HttpEntity entity = new HttpEntity(body, headers);
ResponseEntity<JSONObject> exchange = restTemplate.exchange("https://api.agora.io/dev/v1/channel/{appid}",
HttpMethod.GET, entity, JSONObject.class, appid);
return exchange.getBody();
}
}
最后
以上就是欢呼胡萝卜为你收集整理的声网的使用的全部内容,希望文章能够帮你解决声网的使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复