(可隱藏)傳送圖文訊息
更新時間:2022-09-02 18:08:07
介面描述
1、通过聊天室API,发送图文信息
2、接口支持https协议
3、接口URL中的{channelId}为"频道号"
介面URL
http://api.polyv.net/live/v1/channelSetting/{channelId}/send-chat
請求方式
POST
介面限制
1、介面同時支援HTTP、HTTPS,建議使用HTTPS以確保介面安全,介面呼叫有頻率限制,詳細請查看
請求參數說明
| 參數名 | 必選 | 類型 | 說明 |
|---|---|---|---|
| appId | true | String | 帳號appId【詳見取得金鑰】 |
| timestamp | true | Long | 目前13位毫秒級時間戳記,3分鐘內有效 |
| sign | true | String | 簽名,為32位大寫的MD5值,產生簽名的appSecret金鑰作為通訊資料安全的關鍵資訊,嚴禁儲存在用戶端直接使用,所有API都必須透過客戶自己伺服器中轉呼叫POLYV伺服器取得回應資料【詳見簽名產生規則】 |
| userId | true | String | 直播帳號id |
| content/imgUrl | true | String | 需要傳送的圖片或文字,兩者不能同時為空,可以同時提交,content需要進行base64編碼,詳見url安全的base64編碼規則】 |
url安全的base64編碼規則
Java
public static String encodeBase64URLSafeString(String text) {
byte[] data = org.apache.commons.codec.binary.Base64.encodeBase64URLSafe(text.getBytes(StandardCharsets.UTF_8));
return new String(data, StandardCharsets.UTF_8);
}
php
function urlsafe_b64encode($string) {
$data = base64_encode($string);
$data = str_replace(array('+','/','='), array('-','_',''), $data);
return $data;
}
範例
http://api.polyv.net/live/v1/channelSetting/2191569/send-chat
表單參數:
appId=frlr1zazn3&sign=47B9259E6E93CA2E15B33D532E813417&userId=1b448be323&content=5rWL6K-V77yI5Y-v6ZqQ6JeP77yJ5Y-R6YCB5Zu-5paH5L-h5oGv×tamp=1616641094219
回應參數說明
| 參數名 | 類型 | 說明 |
|---|---|---|
| status | String | 回應狀態文字資訊 |
| result | String | 回應資料 |
Java請求範例
快速接入基礎程式碼請下載相關依賴原始碼, 點擊下載原始碼,下載後加入到自己的原始碼工程中即可。測試案例中的HttpUtil.java 和 LiveSignUtil.java 都包含在下載檔案中。
強烈建議您使用直播Java SDK完成API的功能對接,直播Java SDK 對API呼叫邏輯、例外處理、資料簽名、HTTP請求執行緒池進行了統一封裝和最佳化。
private static final Logger log = LoggerFactory.getLogger(ChatMessageTest.class);
// URL安全的字符串base64编码
public static String encodeBase64URLSafeString(String text) {
byte[] data = org.apache.commons.codec.binary.Base64.encodeBase64URLSafe(text.getBytes(StandardCharsets.UTF_8));
return new String(data, StandardCharsets.UTF_8);
}
/**
* (可隐藏)发送图文信息
* @throws IOException
*/
@Test
public void testSendHiddenMessage() throws IOException, NoSuchAlgorithmException {
//公共参数,填写自己的实际参数
String appId = super.appId;
String appSecret = super.appSecret;
String userId = super.userId;
String timestamp = String.valueOf(System.currentTimeMillis());
//业务参数
String channelId = "2191569";
String url = "http://api.polyv.net/live/v1/channelSetting/"+channelId+"/send-chat";
String content = encodeBase64URLSafeString("测试(可隐藏)发送图文信息");
//http 调用逻辑
Map<String,String> requestMap = new HashMap<>();
requestMap.put("appId", appId);
requestMap.put("timestamp",timestamp);
requestMap.put("content",content);
requestMap.put("userId",userId);
requestMap.put("sign",LiveSignUtil.getSign(requestMap, appSecret));
String response = HttpUtil.postFormBody(url, requestMap);
log.info("测试(可隐藏)发送图文信息接口返回值:{}",response);
}
回應範例
系統全域錯誤說明詳見全域錯誤說明
成功範例
{
"status": "success",
"result": ""
}
異常範例
{
"code": 400,
"status": "error",
"message": "invalid signature.",
"data": ""
}
