【问题标题】:Users_not_found in HTTParty request to SlackAPI when curl works当 curl 工作时 HTTParty 请求中的 users_not_found 到 SlackAPI
【发布时间】:2020-01-24 10:30:49
【问题描述】:

我正在尝试通过 httparty gem 使用 GET 请求从 SlackAPI 获取有关特定用户的信息。从 curl 它运行良好

curl --data "token=SLACK_TOKEN&email=user@example.com" https://slack.com/api/users.lookupByEmail

但是我下面的代码似乎被破坏了,因为我收到了一个错误{"ok":false,"error":"users_not_found"}

module Slack
  class GetUserId
    def call
      response = HTTParty.get("https://slack.com/api/users.lookupByEmail", payload: payload, headers: headers)
    end

    def headers
      {
        'Content-Type' => 'application/json',
        'Authorization' => 'Bearer SLACK_TOKEN'
      }
    end

    def payload
      {
         email: "user@example.com"
      }.to_json
    end
  end
end

【问题讨论】:

    标签: ruby slack-api httparty


    【解决方案1】:

    如果您查看他们的文档,似乎 API 不接受 JSON 格式,而是接受“application/x-www-form-urlencoded。

    所以像:

    headers: {
      'Authorization' => 'Bearer SLACK_TOKEN,
       "Content-Type" => "application/x-www-form-urlencoded"
         },
    body: {
         "token=SLACK_TOKEN”,
         ”email=user@example.com" 
        ...
    

    参考:https://api.slack.com/methods/users.lookupByEmail

    【讨论】:

    • 我不这么认为。我想我为 httparty 而不是 GET 定义了 POST 方法,其中参数在 url 但我不知道如何传递它 - github.com/jnunemaker/httparty/blob/master/examples/aaws.rb#L23
    • @Fernand 是正确的。该方法不支持 JSON 编码,仅支持表单编码。但除此之外,这也意味着您必须将令牌放在正文中,而不是在 auth 标头中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多