我有一些python工具,我想要发送更新到hipchat房间。我在其他地方使用shell脚本执行此操作,因此我知道它在我们的环境中可以工作,但我似乎无法将令牌推送到hipchat API。一定要简单一点。
首先,这将正确地进行身份验证并传递一条消息:
curl -d "room_id=xxx&from=DummyFrom&message=ThisIsATest&color=green" https://api.hipchat.com/v1/rooms/message?auth_token=yyy但是当我尝试使用python的“请求”模块时,我被卡住了。
import requests
room_id_real="xxx"
auth_token_real="yyy"
payload={"room_id":room_id_real,"from":"DummyFrom","message":"ThisIsATest","color":"green"}
headerdata={"auth_token":auth_token_real,"format":"json"}
r=requests.post("https://api.hipchat.com/v1/rooms/message", params=payload, headers=headerdata)
print r.ok, r.status_code, r.text以下是我的错误信息:
False 401 {"error":{"code":401,"type":"Unauthorized","message":"Auth token not found. Please see: https:\/\/www.hipchat.com\/docs\/api\/auth"}}基本上,我似乎没有正确地传递身份验证令牌。我怎么才能让它正常工作呢?
发布于 2014-09-20 03:37:46
如果有帮助,这里有一个有效的V2应用编程接口示例。我确实发现V2应用程序接口对准确获取请求的形式更加敏感。但是,遵循V2 API可能更具前瞻性(尽管最初的问题似乎与V1有关)。
#!/usr/bin/env python
import json
from urllib2 import Request, urlopen
V2TOKEN = '--V2 API token goes here--'
ROOMID = --room-id-nr-goes-here--
# API V2, send message to room:
url = 'https://api.hipchat.com/v2/room/%d/notification' % ROOMID
message = "It's a<br><em>trap!</em>"
headers = {
"content-type": "application/json",
"authorization": "Bearer %s" % V2TOKEN}
datastr = json.dumps({
'message': message,
'color': 'yellow',
'message_format': 'html',
'notify': False})
request = Request(url, headers=headers, data=datastr)
uo = urlopen(request)
rawresponse = ''.join(uo)
uo.close()
assert uo.code == 204发布于 2015-03-31 14:09:44
使用请求的另一个基本示例:
import requests, json
amessage = 'Hello World!'
room = 'https://api.hipchat.com/v2/room/18REPLACE35/notification'
headers = {'Authorization':'Bearer UGetYourOwnAuthKey', 'Content-type':'application/json'}
requests.post(url = room, data = json.dumps({'message':amessage}), headers = headers)发布于 2014-03-14 06:02:20
正如Ianzz所说,尝试将其包含在URL查询字符串中。虽然笨拙(你可能想把它散列出来!),但它绝对是有效的。
另一个奇怪的怪现象是你通过Hipchat得到的令牌;今天晚上早些时候,我使用自己的令牌遇到了无数的问题;它似乎与应用编程接口的v2测试版相对应。如果您通过Group Admin进入并从中获取令牌,可能会有所帮助。
老问题是老问题。
https://stackoverflow.com/questions/22020247
复制相似问题