0

我正在使用 Ruby 1.8.7,它不允许我使用最新的 HipChat gem(由于 httparty 不兼容)并且旧版本不起作用。似乎即使是这样的简单脚本也无法完成任务。我无法弄清楚它有什么问题

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}"

产量

Response 401 Unauthorized: {
  "error": {
    "code": 401,
    "message": "Authenticated requests only. See https://www.hipchat.com/docs/apiv2/auth for more information.",
    "type": "Unauthorized"
  }
}

虽然这有效

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

我已经尝试了所有我能想象到的方法,但似乎 HipChat API 没有看到我的令牌。我也尝试通过授权标头发送它,但没有成功..

编辑: 我试图查看 Ruby 实际发送的内容,它完全摆脱了 auth_token 参数。所以我把它作为标题发送

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

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

仍然没有运气。这是捕获的两个 Wireshark 帧。CURL 一个通过了,Ruby 一个没有。

卷曲(成功):

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(不成功)**

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
4

0 回答 0