【问题标题】:Unauthorized readbody=true when posting message to hangout chat room with webhook使用 webhook 向环聊聊天室发布消息时未授权的 readbody=true
【发布时间】:2019-01-31 02:08:20
【问题描述】:

尝试向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 中,我认为这将不需要。

【问题讨论】:

    标签: ruby webhooks hangout


    【解决方案1】:

    这对我有用

    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
    

    【讨论】:

    • 那么,归根结底,不需要身份验证?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 2014-10-31
    相关资源
    最近更新 更多