HipChat API authentication fails when initiated from Ruby script -
i'm using ruby 1.8.7 not allow me use newest hipchat gem (due httparty incompatibility) , older version not work. seems though simple script cannot accomplish task. cannot figure out wrong it
require 'uri' require 'net/http' require 'net/https' require 'json' data = { 'color' => 'green', 'message' => 'yaba-daba-doo!', 'notify' => false }.to_json uri = uri.parse('https://api.hipchat.com/v2/room/room_id/notification?auth_token=my_token') https = net::http.new(uri.host, uri.port) https.use_ssl = true req = net::http::post.new(uri.path, initheader = {'content-type' =>'application/json'}) req.body = "[ #{data} ]" res = https.request(req) puts "response #{res.code} #{res.message}: #{res.body}"
yields
response 401 unauthorized: { "error": { "code": 401, "message": "authenticated requests only. see https://www.hipchat.com/docs/apiv2/auth more information.", "type": "unauthorized" } }
while works
curl -d '{"color": "green", "message": "foobar, damn it!", "notify": false, "message_format": "text"}' -h 'content-type: application/json' https://api.hipchat.com/v2/room/room_id/notification?auth_token=my_token
i've tried every way can imagine , seems hipchat api not see token. tried sending via authorization header, no success..
edit: tried see ruby sending , got rid of auth_token parameter altogether. sent header
headers = { 'content-type' =>'application/json', 'authorization:' => "bearer #{auth_token}" } req = net::http::post.new(uri.path, initheader = headers)
still no luck. here 2 wireshark frames captured. curl 1 passed, ruby 1 didn't.
curl (successful):
frame 458912: 356 bytes on wire (2848 bits), 356 bytes captured (2848 bits) on interface 0 ethernet ii, src: apple_09:01:72 (80:e6:50:09:01:72), dst: routerbo_fa:1e:71 (d4:ca:6d:fa:1e:71) internet protocol version 4, src: 192.168.1.138 (192.168.1.138), dst: 107.21.219.105 (107.21.219.105) transmission control protocol, src port: 65126 (65126), dst port: 80 (80), seq: 1, ack: 1, len: 290 hypertext transfer protocol post /v2/room/room_id/notification http/1.1\r\n [expert info (chat/sequence): post /v2/room/room_id/notification http/1.1\r\n] [post /v2/room/room_id/notification http/1.1\r\n] [severity level: chat] [group: sequence] request method: post request uri: /v2/room/room_id/notification request version: http/1.1 host: api.hipchat.com\r\n user-agent: curl/7.43.0\r\n accept: */*\r\n content-type: application/json\r\n authorization: bearer auth_token\r\n content-length: 66\r\n [content length: 66] \r\n [full request uri: http://api.hipchat.com/v2/room/room_id/notification] [http request 1/1] [response in frame: 458920] javascript object notation: application/json object member key: "color" string value: green member key: "message" string value: foobar, damn it! member key: "notify" false value
** ruby (unsuccessful) **
frame 453567: 127 bytes on wire (1016 bits), 127 bytes captured (1016 bits) on interface 0 ethernet ii, src: apple_09:01:72 (80:e6:50:09:01:72), dst: routerbo_fa:1e:71 (d4:ca:6d:fa:1e:71) internet protocol version 4, src: 192.168.1.138 (192.168.1.138), dst: 107.21.219.105 (107.21.219.105) transmission control protocol, src port: 65124 (65124), dst port: 80 (80), seq: 220, ack: 1, len: 61 [2 reassembled tcp segments (280 bytes): #453562(219), #453567(61)] hypertext transfer protocol post /v2/room/room_id/notification http/1.1\r\n [expert info (chat/sequence): post /v2/room/room_id/notification http/1.1\r\n] [post /v2/room/room_id/notification http/1.1\r\n] [severity level: chat] [group: sequence] request method: post request uri: /v2/room/room_id/notification request version: http/1.1 content-type: application/json\r\n connection: close\r\n accept: */*\r\n content-length: 61\r\n [content length: 61] authorization:: bearer auth_token\r\n host: api.hipchat.com\r\n \r\n [full request uri: http://api.hipchat.com/v2/room/room_id/notification] [http request 1/1] [response in frame: 453576] javascript object notation: application/json object member key: "message" string value: foobar, damn it! member key: "color" string value: green member key: "notify" false value
Comments
Post a Comment