RestAPI
接口地址
获取用户token
- 第三方应用的客户端通过应用服务器作为桥梁获取连接im服务器使用到的token
- 从im api服务器返回的用户token是永久有效的
接口规范
接口支持4种HTTP方法
- GET 获取对象
- PUT 替换和更新对象
- POST 创建新的对象
- DELETE 删除对象
HTTP返回码
- 200 成功
- 其他 失败
HTTP 接口返回值结构
{
"meta":
{
"code": "业务码",
"message": "状态信息"
},
"data": ...
}
*若无特殊说明以下接口响应结果指的是此结构的data字段的值。*
第三方服务端授权
- 请求地址: 所有可供第三方服务端访问的接口
- 请求头部: Authorization: Basic $base64(appid:appsecret)
第三方应用获取永久有效的access token
- 请求地址: POST /auth/grant
- 是否认证:服务端授权
请求内容:
{ "uid":"用户id(整型)" "name":"用户昵称" }
成功响应:200
{ "token":"访问token" }
操作失败: 400 非法参数
设置用户名
- 请求地址: POST /users/{uid}
- 是否认证:服务端授权
请求内容:
{ "name":"用户名" }
成功响应:200
操作失败: 400 非法参数
创建群组
- 请求地址:POST /groups
- 是否认证:服务端授权
- 请求头:Content-Type:application/json
- 请求内容:
{
"master":"管理员id(整型)",
"name":"群主题名",
"members":[
{
"uid":"用户id(整型)",
"name":"用户昵称"
},
...
]
}
成功响应:200
{
"group_id":"群组id(整型)"
}
- 操作失败: 400 非法的输入参数
修改群组名称
- 请求地址:PATCH /groups/{gid}
- 是否认证:服务端授权
- 请求头:Content-Type:application/json
- 请求内容:
{
"name":"群主题名",
}
成功响应:200
操作失败: 400 非法的输入参数
解散群组
- 请求地址:DELETE /groups/{gid}
- 是否认证:服务端授权
- 成功响应:200
- 操作失败: 400 非法的群id
添加群组成员
- 请求地址:POST /groups/{gid}/members
- 是否认证:服务端授权
- 请求头:Content-Type:application/json
- 请求内容:
[
{
"uid":"用户id(整型)",
"name":"用户昵称"
},
...
]
- 成功响应:200
- 操作失败: 400 非法的群成员id
移除群成员
- 请求地址:DELETE /groups/{gid}/members
- 是否认证:服务端授权
- 请求头:Content-Type:application/json
请求内容:
[
{ "uid":"用户id(整型)", "name":"用户昵称" }, ...
]
成功响应:200
- 操作失败: 400 非法参数
java获取token
/**
*
* 该demo依赖net.sf.json jar包
*
*/
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONFunction;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import java.io.IOException;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
public class GoBelieveIO {
private static final int TIMEOUT_IN_MILLIONS = 5000;
private static final String API = "https://api.gobelieve.io/v2";
//APPID为long型
private static final long appID = 999999;
//APP_SECRET为字符串
private static final String appSecret = "999999999999999999";
private static String base64Encode(byte[] c) {
Base64.Encoder encoder = Base64.getEncoder();
return encoder.encodeToString(c);
}
public static String getBasicAuth(long appID, String appSecret) {
String t = "" + appID + ":" + appSecret;
final String encodedText = base64Encode(t.getBytes());
return encodedText;
}
/**
* @param uid long型
* @param userName 字符串
*/
public static String doAuthGrant(long uid, String userName) {
String url = String.format("%s/auth/grant", API);
OutputStream out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/json");
conn.setRequestProperty("charset", "utf-8");
String basicAuth = getBasicAuth(appID, appSecret);
conn.setRequestProperty("Authorization", "Basic " + basicAuth);
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setReadTimeout(TIMEOUT_IN_MILLIONS);
conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);
JSONObject json = new JSONObject();
json.put("uid", uid);
json.put("user_name", userName);
String param = json.toString();
out = conn.getOutputStream();
out.write(param.getBytes("utf8"));
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null){
result += line;
}
System.out.println("result:" + result);
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if (out != null){
out.close();
}
if (in != null){
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
public static void main(String[] args) {
doAuthGrant(1, "11");
}
}
python 2.7获取token
# -*- encoding=utf8 -*-
#
# 依赖requsts包
#
__author__ = "gobelive"
import base64
import json
import requests
def grant():
#APPID为long型
APP_ID = 999999
#APP_SECRET为字符串
APP_SECRET = "999999999999999999"
#uid为long型
uid = 333
#user_name为字符串
userName = "aaabbb"
url = "https://api.gobelieve.io/v2/auth/grant"
obj = {"uid":uid, "user_name":userName}
auth = str(APP_ID) + ":" + APP_SECRET
basic = base64.b64encode(auth.encode())
headers = {'Content-Type':'application/json;charset=UTF-8','Authorization':'Basic ' + str(basic)}
res = requests.post(url, data=json.dumps(obj), headers=headers)
if res.status_code == 200:
obj = json.loads(res.text)
print(obj)
token = obj["data"]["token"]
print(token)
grant()
node js获取token
//依赖request包
const request = require('request');
//APPID为long型
var appId = 999999;
//APP_SECRET为字符串
var appSecret = "999999999999999999";
var auth = appId + ":" + appSecret;
auth = new Buffer(auth).toString('base64');
auth = "Basic " + auth;
//user_name为字符串
var name = "333";
//uid为long型
var userId = 3333;
request({
url: 'https://api.gobelieve.io/v2/auth/grant',
method: 'POST',
json: true,
headers: {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': auth
},
body: { "user_name": name, "uid": userId }
}, function (err, res1, body) {
if (err) {
console.log(err);
} else {
console.log(body);
}
});
php获取token
$url = "https://api.gobelieve.io/v2/auth/grant";
$app_id = 0;
$app_secret = '';
$auth = base64_encode(sprintf('%s:%s', $app_id, $app_secret));
$header = array("Content-type: application/json", 'Authorization: Basic ' . $auth);
$data = json_encode(array('uid' => (int)$uid, 'user_name' => $user_name));
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//SSL certificate problem: unable to get local issuer certificate. (cURL error code 60)
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
curl_close($curl);
$json = json_decode($result, true);
$gobelieve_token = $json['data']['token'];
echo "token:", $gobelieve_token, "\n";