环信的REST API都是基于json的, 所以在构造_HTTP_ 请求的时候, 需要在_HTTP HEADER_中指明:
header_name | header_value | description |
Accept | application/json | 服务器端返回给客户端的数据类型 |
Content-Type | application/json | 客户端发到服务器端的是数据类型 |
Path : URI HTTP Method : GET/POST/PUT/DELETE Request Headers : { Content-Type : application/json, Accept: application/json, Authorization : Bearer ${token} } Request Body : JSON
名词 | 解释 |
org_name | 企业的唯一标识,开发者在环信开发者管理后台注册账号时填写的企业ID |
app_name | 同一”企业”下”app”唯一标识,开发者在环信开发者管理后台创建应用时填写的”应用名称” |
org_admin | 开发者在环信开发者管理后台注册时填写的”用户名”.企业管理员拥有对该企业账号下所有资源的操作权限 |
app_admin | 应用管理员,具有admin权限的一个特殊IM用户,拥有对该应用下所有资源的操作权限 |
appkey | 一个app的唯一标识,规则是 ${org_name}#${app_name} |
Path : /{org_name}/{app_name}/token HTTP Method : POST URL Params : 无 Request Headers : {“Content-Type”:”application/json”} Request Body : {“grant_type”: “client_credentials”,”client_id”: “{app的client_id}”,”client_secret”: “{app的client_secret}”}
key | value |
access_token | token值 |
expires_in | 有效时间,秒为单位, 默认是七天,在有效期内是不需要重复获取的 |
application | 当前app的UUID值 |
Path : /{org_name}/{app_name}/users HTTP Method : POST URL Params : 无 Request Headers : {“Content-Type”:”application/json”} Request Body : {“username”:”${用户名}”,”password”:”${密码}”, “nickname”:”${昵称值}”}
Path : /{org_name}/{app_name}/users HTTP Method : POST URL Params : 无 Request Headers : { “Content-Type”:”application/json”, ”Authorization”:”Bearer ${token}” } Request Body : {“username”:”${用户名}”,”password”:”${密码}”}
Path : /{org_name}/{app_name}/users HTTP Method : POST URL Params : 无 Request Headers : {“Content-Type”:”application/json”,”Authorization”:”Bearer ${token}”} Request Body : [{“username”:”${用户名1}”,”password”:”${密码}”},…,{“username”:”${用户名2}”,”password”:”${密码}”}]
{ "action" : "post", "application" : "4d7e4ba0-dc4a-11e3-90d5-e1ffbaacdaf5", "params" : { }, "path" : "/users", "uri" : "https://a1.easemob.com/easemob-demo/chatdemoui/users", "entities" : [ entitie,entitie,entitie,entitie ], "timestamp" : 1409570811312, "duration" : 802, "organization" : "easemob-demo", "applicationName" : "chatdemoui" }
{ "uuid" : "de86365a-31ca-11e4-aecf-9509b836c0d6", "type" : "user", "created" : 1409570811445, "modified" : 1409570811445, "username" : "u2", "activated" : true }entitie
Path : /{org_name}/{app_name}/users/{username} HTTP Method : GET Request Headers : {“Authorization”:”Bearer ${token}”}
Path : /{org_name}/{app_name}/users HTTP Method : GET //指定条数 URL Params : limit=20 //cursor:用户开始索引 URL Params : limit=20&cursor=LTU2ODc0MzQzOnNmdTlxdF9LRWVPaVFvMWlBZmc4S3c Request Headers : {“Authorization”:”Bearer ${token}”}
{ "action" : "get", "application" : "4d7e4ba0-dc4a-11e3-90d5-e1ffbaacdaf5", "params" : { "limit" : [ "20" ] }, "path" : "/users", "uri" : "https://a1.easemob.com/easemob-demo/chatdemoui/users?ql=select+*+from+null&limit=20", "entities" : [ entitie,entitie,entitie,entitie ] } { "uuid" : "fff15c10-df37-11e3-843f-e5b88d483c56", "type" : "user", "created" : 1400491736144, "modified" : 1409055655016, "username" : "wjglpgecxu", "activated" : true, "nickname" : "wjglpgecxu", "notifier_name" : "chatdemoui_dev" }entitie
Path : /{org_name}/{app_name}/users/{username} HTTP Method : DELETE URL 参数 : 无 Request Headers : {“Authorization”:”Bearer ${token}”} Request Body :无
{ "action" : "delete", "application" : "4d7e4ba0-dc4a-11e3-90d5-e1ffbaacdaf5", "params" : { }, "path" : "/users", "uri" : "https://a1.easemob.com/easemob-demo/chatdemoui/users", "entities" : [ entitie,entitie,entitie,entitie ], "timestamp" : 1409576121910, "duration" : 3330, "organization" : "easemob-demo", "applicationName" : "chatdemoui" } { "uuid" : "628a88ba-dfce-11e3-8cac-51d3cb69b303", "type" : "user", "created" : 1400556326075, "modified" : 1400556326075, "username" : "ywuxvxuir6", "activated" : true }entitie
DELETE /{org_name}/{app_name}/users?ql=created > 1409506121910 and created < 1409576121910 Path : /{org_name}/{app_name}/users HTTP Method : DELETE URL Params : limit=30 Request Headers : {“Authorization”:”Bearer ${token}”}
{ "action" : "delete", "application" : "4d7e4ba0-dc4a-11e3-90d5-e1ffbaacdaf5", "params" : { "limit" : [ "5" ] }, "path" : "/users", "uri" : "https://a1.easemob.com/easemob-demo/chatdemoui/users", "entities" : [] }
Path : /{org_name}/{app_name}/users/{username}/password HTTP Method : PUT URL Params : 无 Request Headers : {“Authorization”:”Bearer ${token}”} Request Body : {“newpassword” : “${新密码指定的字符串}”}
{ "action" : "set user password", "timestamp" : 1409575962124, "duration" : 326 }
Path : /{org_name}/{app_name}/users/{username} HTTP Method : PUT URL Params : 无 Request Headers : {“Authorization”:”Bearer ${token}”} Request Body : {“nickname” : “${昵称值}”}
{ "action" : "put", "application" : "4d7e4ba0-dc4a-11e3-90d5-e1ffbaacdaf5", "path" : "/users", "uri" : "https://a1.easemob.com/easemob-demo/chatdemoui/users", "entities" : [ { "uuid" : "c3b56d5a-7135-11e4-92d2-edab82ae2302", "type" : "user", "created" : 1416543645861, "modified" : 1416550240537, "username" : "jianguo", "activated" : true, "device_token" : "61491f49f3e69cd1d62c5b390e42f4b1cd15bf1a876a487268cfaef9960188ee", "nickname" : "张建国" } ], "timestamp" : 1416550240285, "duration" : 278, "organization" : "easemob-demo", "applicationName" : "chatdemoui" }
Path : /{org_name}/{app_name}/users/{owner_username}/contacts/users/{friend_username} HTTP Method : POST Request Headers : {“Authorization”:”Bearer ${token}”}
{ "action":"post","application":"4d7e4ba0-dc4a-11e3-90d5-e1ffbaacdaf5","params":{}, "path":"/users/aa6160da-eb01-11e3-ab09-15edd986e7b7/contacts", "uri":"http://a1.easemob.com/easemob-demo/chatdemoui/users/jliu/contacts/yantao", "entities":[ entitie ], "timestamp":1406086326974,"duration":242, "organization":"easemob-demo", "applicationName":"chatdemoui" }
Path : /{org_name}/{app_name}/users/{owner_username}/contacts/users HTTP Method : GET Request Headers : {“Authorization”:”Bearer ${token}”} { "action" : "get", "application" : "4d7e4ba0-dc4a-11e3-90d5-e1ffbaacdaf5", "params" : { }, "uri" : "https://a1.easemob.com/easemob-demo/chatdemoui/users/v3y0kf9arx/contacts/users", "entities" : [ ], "data" : [ "88888" ], "timestamp" : 1409737366071, "duration" : 45, "organization" : "easemob-demo", "applicationName" : "chatdemoui" }
Path : /{org_name}/{app_name}/users/{owner_username}/contacts/users/{friend_username} HTTP Method : DELETE URL Params : 无 Request Headers : {“Authorization”:”Bearer ${token}”} Request Body : 无 { "action" : "delete", "application" : "4d7e4ba0-dc4a-11e3-90d5-e1ffbaacdaf5", "params" : { }, "path" : "/users/stliu/contacts", "uri" : "https://a1.easemob.com/easemob-demo/chatdemo/users/stliu/contacts/users/yantao", "entities" : [ entitie ], "timestamp" : 1409739808288, "duration" : 1575, "organization" : "easemob-demo", "applicationName" : "chatdemoui" }
<pre name="code" class="java">Path : /{org_name}/{app_name}/chatgroups HTTP Method : GET Request Headers : {“Authorization”:”Bearer ${token}”}
Path : /{org_name}/{app_name}/users/{username}/joined_chatgroups HTTP Method : GET Request Headers : {“Authorization”:”Bearer ${token}”}
{ ... "entities" : [ ], "data" : [ { "groupid" : "1413193977786197", "groupname" : "kenshingrou" }, { "groupid" : "1413194058403881", "groupname" : "kenshinngr1" } ], "timestamp" : 1413428676499, "duration" : 80 }
Path : /{org_name}/{app_name}/chatgroups/{group_id1},{group_id2} HTTP Method : GET Request Headers : {“Authorization”:”Bearer ${token}”}
{ ... "entities" : [ ], "data" : [ { "id" : "1408518613503", "name" : "Jay13800138000", "description" : "", "public" : false, "membersonly" : true, "allowinvites" : false, "maxusers" : 200, "affiliations_count" : 3, "affiliations" : [ { "owner" : "13800138001" }, { "member" : "v3y0kf9arx" }, { "member" : "xc6xrnbzci" } ] } ], ... }
Path : /{org_name}/{app_name}/chatgroups/{group_id}/users HTTP Method : GET Request Headers : {“Authorization”:”Bearer ${token}”}
{ ... "entities" : [ ], "data" : [ { "member" : "lidis" }, { "member" : "asdfgh" }, { "owner" : "ruson" } ], "timestamp" : 1413012431449, "duration" : 24 }
Path : /{org_name}/{app_name}/chatgroups HTTP Method : POST URL Params : 无 Request Headers : {“Authorization”:”Bearer ${token}”} Request Body : { "groupname":"testrestgrp12", //群组名称, 此属性为必须的 "desc":"server create group", //群组描述, 此属性为必须的 "public":true, //是否是公开群, 此属性为必须的 "maxusers":300, //群组成员最大数(包括群主), 值为数值类型,默认值200,此属性为可选的 "approval":true, //加入公开群是否需要批准, 没有这个属性的话默认是true, 此属性为可选的 "owner":"jma1", //群组的管理员, 此属性为必须的 "members":["jma2","jma3"] //群组成员,此属性为可选的,但是如果加了此项,数组元素至少一个 }Response 示例:
{ "action" : "post", "application" : "4d7e4ba0-dc4a-11e3-90d5-e1ffbaacdaf5", "params" : { }, "uri" : "https://a1.easemob.com/easemob-demo/chatdemoui", "entities" : [ ], "data" : { "groupid" : "1411527886490154" }, "timestamp" : 1411527886457, "duration" : 125, "organization" : "easemob-demo", "applicationName" : "chatdemoui" }
Path : /{org_name}/{app_name}/chatgroups/{group_id} HTTP Method : DELETE URL Params : 无 Request Headers : {“Authorization”:”Bearer ${token}”} Request Body :无
{ "action" : "delete", "application" : "4d7e4ba0-dc4a-11e3-90d5-e1ffbaacdaf5", "params" : { }, "uri" : "https://a1.easemob.com/easemob-demo/chatdemoui", "entities" : [ ], "data" : { "success" : true, "groupid" : "1411527886490154" }, "timestamp" : 1411528112078, "duration" : 15, "organization" : "easemob-demo", "applicationName" : "chatdemoui" }
Path : /{org_name}/{app_name}/chatgroups/{group_id}/users/{user_primary_key} HTTP Method : POST URL Params : 无 Request Headers : {“Authorization”:”Bearer ${token}”} Request Body :无
{ ... "entities" : [ ], "data" : { "action" : "add_member", "result" : true, "groupid" : "1411816013089", "user" : "q4xpsfjfvf" }, ... }
Path : /{org_name}/{app_name}/chatgroups/{chatgroupid}/users HTTP Method : POST URL Params : 无 Request Headers : {“Authorization”:”Bearer ${token}”} Request Body :{“usernames”:[“username1”,”username2”]}’ — usernames固定属性, 作为json的KEY;username1/username2 要添加到群中的成员用户名,可变
Path : /{org_name}/{app_name}/chatgroups/{group_id}/users/{user_primary_key} HTTP Method : DELETE URL Params : 无 Request Headers : {“Authorization”:”Bearer ${token}”} Request Body :无 Response 示例: { ... "entities" : [ ], "data" : { "action" : "remove_member", "result" : true, "groupid" : "1411816013089", "user" : "q4xpsfjfvf" }, ... "organization" : "easemob-demo", "applicationName" : "chatdemoui" }
{ ... "data": { "stliu": "online" //注意, 这里返回的是用户名和在线状态的键值对, 值为 online 或者 offline }, ... }
{ "target_type" : "users", // users 给用户发消息, chatgroups 给群发消息 "target" : ["u1", "u2", "u3"], // 注意这里需要用数组,数组长度建议不大于20, 即使只有一个用户, // 也要用数组 ['u1'], 给用户发送时数组元素是用户名,给群组发送时 // 数组元素是groupid >>发送文本 "msg" : { "type" : "txt", "msg" : "hello from rest" //消息内容,(http://developer.easemob.com/docs/emchat/rest/chatmessage.html)里的bodies内容 }, >>发送图片 "msg" : { "type" : "img", // 消息类型 "url": "https://a1.easemob.com/easemob-demo/chatdemoui/chatfiles/55f12940-64af-11e4-8a5b-ff2336f03252", //成功上传文件返回的uuid "filename": "24849.jpg", // 指定一个文件名 "secret": "VfEpSmSvEeS7yU8dwa9rAQc-DIL2HhmpujTNfSTsrDt6eNb_" // 成功上传文件后返回的secret }, >>发送语音 "msg" : { "type": "audio", // 消息类型 "url": "https://a1.easemob.com/easemob-demo/chatdemoui/chatfiles/1dfc7f50-55c6-11e4-8a07-7d75b8fb3d42", //成功上传文件返回的uuid "filename": "messages.amr", // 指定一个文件名 "length": 10, "secret": "Hfx_WlXGEeSdDW-SuX2EaZcXDC7ZEig3OgKZye9IzKOwoCjM" // 成功上传文件后返回的secret }, >>发送视频 "msg": { //消息内容 "type": "video",// 消息类型 "filename": "1418105136313.mp4",// 视频文件名称 "thumb": "http://a1.easemob.com/easemob-demo/chatdemoui/chatfiles/67279b20-7f69-11e4-8eee-21d3334b3a97",//成功上传视频缩略图返回的uuid "length": 10,//视频播放长度 "secret": "VfEpSmSvEeS7yU8dwa9rAQc-DIL2HhmpujTNfSTsrDt6eNb_",// 成功上传视频文件后返回的secret "file_length": 58103,//视频文件大小 "thumb_secret": "ZyebKn9pEeSSfY03ROk7ND24zUf74s7HpPN1oMV-1JxN2O2I",// 成功上传视频缩略图后返回的secret "url": "http://a1.easemob.com/easemob-demo/chatdemoui/chatfiles/671dfe30-7f69-11e4-ba67-8fef0d502f46"//成功上传视频文件返回的uuid }, >>发送透传 "msg":{ //消息内容 "type":"cmd", // 消息类型 "action":"action1" }, "from" : "jma2", //表示这个消息是谁发出来的, 可以没有这个属性, 那么就会显示是admin, 如果有的话, 则会显示是这个用户发出的 "ext" : { //扩展属性, 由app自己定义.可以没有这个字段,但是如果有,值不能是“ext:null“这种形式,否则出错 "attr1" : "v1", "attr2" : "v2" } }
{ ... "data": { "stliu1": "success", "jma3": "success", "stliu": "success", "jma4": "success" }, ... }