首页 > 解决方案 > 当 curl 工作时 HTTParty 请求中的 Users_not_found 到 SlackAPI

问题描述

我正在尝试使用 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

标签: rubyslack-apihttparty

解决方案


如果您查看他们的文档,似乎 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


推荐阅读