ruby - 当 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
解决方案
如果您查看他们的文档,似乎 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"
...
推荐阅读
- mule - 在 Mule 4 中设置默认服务器时区
- python - 阻止 json.loads 将 true 或 false 大写为值
- amazon-web-services - 默认值 AWS 替换为伪参数参考
- .net-core - 如何使用支付 ID 和帐户 ID(.Net)从条带中获取传输数据
- python - 将句子转换为 Pig Latin
- android - 使用新的 App Startup API 初始化实例
- javascript - 让 JavaScript 立即调用 .then 中的函数
- vb.net - vb.net 两个 BindingSource 为两个 DataGridView
- javascript - 如何在 React JS 的 componentDidMount 挂钩中进行额外的 api 调用
- python - 为什么我的脚本超时?- BeautifulSoup 超时错误