2

I have some python tools that I would like to have send updates to a hipchat room. I do this elsewhere with shell scripts, so I know it works in our environment, but I can't seem to get the token pushed to the hipchat API. Gotta be something simple.

First, this authenticates properly and delivers a message:

curl -d "room_id=xxx&from=DummyFrom&message=ThisIsATest&color=green" https://api.hipchat.com/v1/rooms/message?auth_token=yyy

But when I try to use the python "requests" module, I am getting stuck.

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

Here is my error information:

False 401 {"error":{"code":401,"type":"Unauthorized","message":"Auth token not found. Please see: https:\/\/www.hipchat.com\/docs\/api\/auth"}}

Basically I don't seem to be passing the authentication token in properly. How can I get this working?

Sam Whited
  • 6,880
  • 2
  • 31
  • 37
Skip Huffman
  • 5,239
  • 11
  • 41
  • 51
  • 1
    In your `curl` example you're passing the authentication token in a query string parameter, while in your Python example you're passing it as a header value. Since your `curl` submission works, have you tried do it the same way in Python, by including the token in the URL query string? – lanzz Feb 25 '14 at 16:32

4 Answers4

4

In case it helps, here's a working V2 API example. I did find the V2 API to be a bit more sensitive about getting the form of the request exactly right. But, it might be more forward-looking to conform to the V2 API (though the original question seemed to pertain to 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
Jason Drew
  • 281
  • 2
  • 8
3

Another basic example using requests:

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)
Joel Bondurant
  • 841
  • 1
  • 10
  • 17
1

As Ianzz said, try including it in the URL query string. Although clunky (you probably want to hash it!), it definitely works.

The other strange quirk is the tokens that you get through Hipchat; I had no end of problems earlier this evening using my own personal token; it seemed to correspond to v2 beta of the API. If you go in through Group Admin and get a token from there, it may help.

Old question is old.

Withnail
  • 3,128
  • 2
  • 30
  • 47
  • Not accepting this answer because I think a better one is (or at least should be) out there. But this is exactly what I am doing right now. – Skip Huffman Mar 17 '14 at 12:16
-1

Here's an official list of libs which use the HipChat API v2 interface https://www.hipchat.com/docs/apiv2/libraries

hamx0r
  • 4,081
  • 1
  • 33
  • 46