使用Webhook将消息发布到环聊聊天室时,未经授权的readbody = true

时间:2019-01-31 02:08:20

标签: ruby webhooks hangout

尝试将消息发布到hangout chat room。我为房间生成了一个Webhook,并在以下代码中将其用作uri。其余的都是基本的net / http内容。

require 'net/http'
require 'uri'

message = 'hello'

# prep and send the http request 
uri = URI.parse("https://chat.googleapis.com/v1/spaces/AAAAcroWtl4/messages?key=abc&token=xyz")

request = Net::HTTP::Post.new(uri)
request.content_type = "application/json"
request.body = '"content":[{"type":"text","text":"'+message+'"}]'
req_options = { use_ssl: uri.scheme == "https" }

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
  http.request(request)
end
puts response.inspect

响应包含以下文本。

#<Net::HTTPUnauthorized 401 Unauthorized readbody=true>

请求正文是否有问题?

编辑:问题中的密钥和令牌已更改。

同一代码可用于跨步室,唯一的区别是它具有

request["Authorization"] = "Bearer #{access_token}"

由于令牌已经在uri中,因此我认为这是不需要的。

1 个答案:

答案 0 :(得分:0)

这对我有用

require 'net/http'
require 'uri'
require 'json'

message = 'hello'

# prep and send the http request 
uri = URI.parse("//webhook")
request = Net::HTTP::Post.new(uri)
request['Content-Type'] = "application/json"
request.body = { text: message }.to_json

response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
  http.request(request)
end

puts response.inspect