【问题标题】:Implement curl request in R在 R 中实现 curl 请求
【发布时间】:2021-09-30 05:08:29
【问题描述】:

我正在尝试访问 Amadeus travel API

要获得一个token,给定的curl是:

curl "https://test.api.amadeus.com/v1/security/oauth2/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}"

我的 RScript 尝试是:

library("httr")

# Get Token
response <- POST("https://test.api.amadeus.com/v1/security/oauth2/token",
                 add_headers("Content-Type" = "application/x-www-form-urlencoded"),
                 body = list(
                   "grant_type"     = "client_credentials",
                   "client_id"      = API_KEY,
                   "client_secret"  = API_SECRET),
                 encode = "json")
response
rsp_content <- content(response, as = "parsed", type = "application/json")
rsp_content

导致错误:

Response [https://test.api.amadeus.com/v1/security/oauth2/token]
  Date: 2021-07-23 00:59
  Status: 400
  Content-Type: application/json
  Size: 217 B

        {
            "error":"invalid_request",
            "error_description": "Mandatory grant_type form parameter missing",
            "code": 38187,
            "title": "Invalid parameters"
        }
> 

调用此 API 以使用 R 获取令牌的正确方法是什么?

【问题讨论】:

  • encode = "json" 似乎不正确。试试encode="form"
  • 你是对的。谢谢!

标签: r curl httr rcurl


【解决方案1】:

curl -d 选项用于以与 HTML 表单相同的方式发送数据。要匹配该格式,请在对 POST() 的调用中使用 encode="form" 而不是 encode="json"

【讨论】:

    猜你喜欢
    • 2013-08-24
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多