使用 httr 创建 POST 调用 Bearer 令牌

Use httr to create POST call for Bearer token

我正在使用 R.

我是 API 的新手,正在尝试弄清楚如何将 post 调用放在一起以获得所需的不记名令牌。 我正在使用 Experian 沙盒。

一旦我有了不记名令牌,我就很好了,但事实证明,将 Post 呼叫放在一起对我来说非常困难。

+以下是从开发者门户网站上截取的。

The call to get the Oauth2 token is a POST request with a Content-Type which needs to be specified as JSON; the response will also be in JSON format:

请求示例:

curl -X POST
-d '{"username": "youremail@email.com", "password": "YOURPASSWORD"}'
-H "Client_id: xxxxxxxxxxxxxxxxxxxxxxxx"
-H "Client_secret: xxxxxxxxx"
-H "Cache-Control: no-cache"
-H "Content-Type: application/json"
"https://sandbox-us-api.experian.com/oauth2/v1/token"

如果其他人需要它以供将来参考,以下解决方案可以解决我的问题。感谢 R 社区帮助我了解此调用的最新执行方式。

post_req <- httr::POST(
  "https://sandbox-us-api.experian.com/oauth2/v1/token",
  add_headers(
    "Content-Type" = "application/json", 
    "Cache-Control"="no-cache",
    "Client_secret"="xxxxxxxxxx",
    "Client_id"="xxxxxxxxxxxxxxxxx"),
  body = '{"username": "youremail@email.com", "password": "YOURPASSWORD"}',
  verbose()
)