HipChat API authentication fails when initiated from Ruby script

374 views Asked by At

I'm using Ruby 1.8.7 which does not allow me to use the newest HipChat gem (due to httparty incompatibility) and an older version does not work. It seems though that even a simple script like this cannot accomplish the task. I cannot figure out what is wrong with 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 for more information.",
    "type": "Unauthorized"
  }
}

while this 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 it every way I can imagine and it just seems that HipChat API does not see my token. I also tried sending it via Authorization header, but to no success..

EDIT: I tried to see what Ruby is actually sending and it got rid of the auth_token parameter altogether. So I sent it as a header

headers = {
  'Content-Type' =>'application/json',
  'Authorization:' => "Bearer #{auth_token}"
}

req = Net::HTTP::Post.new(uri.path, initheader = headers)

Still no luck. Here are two Wireshark frames captured. The CURL one passed, the Ruby one 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
0

There are 0 answers