python3 get post请求Yunba RESTful API
一:主要内容
- 获取云巴appkey和seckey
- 状态回复说明
- get请求
- post请求
二:获取云巴appkey和seckey
1. 注册云巴
在云巴官网,注册一个云巴账号,官网地址:https://yunba.io/
2. 创建应用
注册账号成功后,会跳转到我的应用列表页面,点击创建应用,输入应用名称和应用包名,点击确定创建应用
2. 查看appkey和seckey
应用创建成功后,点击应用名称,可以查看该应用详情信息,就可以看到appkey和seckey了
三:状态回复说明
1. 发送成功
{"status":0, "messageId": "<message-id>"}
2. 参数错误
{"status":1, "error": "invalid parameters"}
3.内部服务错误
{"status":2, "error": "internal server"}
4.没有应用
{"status":3}
5.发布超时
{"status":4, "error": "timeout"}
6.没有找到 Alias
{"status":5, "alias":"567a4a754407a3cd028aaf6b-test", "error": "alias not found"}
四:get请求
#encoding=utf8
import requests
#Get method encapsulation
def getWebsocket(msg,result):
data ={
"method": "publish",
"appkey": "这里填你自己的应用的appkey,就是上面二中获取的数据",
"seckey": "这里填你自己的应用的seckey,就是上面二中获取的数据",
"topic": "这里填频道的名称,如channel1",
"msg": msg
}
results = requests.get(url='http://rest.yunba.io:8080', params=data)
print(results.content)
resultdata = results.json()
resultcode = resultdata['status']
if (0 == resultcode):
print("Connect websocket to "+result+" messages successfully!")
else:
print("Connecting websocket to "++result++" messages failed!")
if __name__ == "__main__":
#发送消息
getWebsocket("interfacetest","send msg")
四:post请求
#encoding=utf8
import json
import requests
#post method encapsulation
def postWebsocket(msg,result):
data ={
"method": "publish",
"appkey": "这里填你自己的应用的appkey,就是上面二中获取的数据",
"seckey": "这里填你自己的应用的seckey,就是上面二中获取的数据",
"topic": "这里填频道的名称,如channel1",
"msg": msg
}
headers = {'Content-Type':'application/json'}
values = json.dumps(data)
results = requests.post('http://rest.yunba.io:8080', data=values, headers=headers)
print(results.content)
resultdata = results.json()
resultcode = resultdata['status']
if (0 == resultcode):
print("Connect websocket to "+result+" messages successfully!")
else:
print("Connecting websocket to "+result+" messages failed!")
if __name__ == "__main__":
#发送图片
postWebsocket("{'img': 'https://www.baidu.com/img/baidu_jgylogo3.gif'}","send picture")
#发送视频
postWebsocket("{'video':'https://vjs.zencdn.net/v/oceans.mp4'}", "send video")