node.js - 如何修复 grant_type 错误为 NULL - Pay Pal 的 REST API 不支持访问 token 的授权类型

标签 node.js paypal access-token paypal-rest-sdk superagent

我正在调用 Paypal 的 REST API 来获取访问 token https://developer.paypal.com/docs/api/overview/#get-an-access-token

我正在使用 Node superagent进行 AJAX 调用。

这是我的代码:

const basicAuth = Buffer.from(`${PAYPAL_CLIENT}:${PAYPAL_SECRET}`).toString('base64')

request
  .post(PAYPAL_OAUTH_API)
  .set('Accept', 'application/json')
  .set('grant_type', 'client_credentials')
  .set('Authorization', `Basic ${basicAuth}`)
  .send()
  .end((result) => {
    console.log(result.response.error)
  })

这是我遇到的错误的日志


{ Error: cannot POST /v1/oauth2/token/ (400)
    at Response.toError (C:\Users\shlomo\projects\tribewise-backend-v1\node_modules\superagent\lib\node\response.js:94:15)
    at ResponseBase._setStatusProperties (C:\Users\shlomo\projects\tribewise-backend-v1\node_modules\superagent\lib\response-base.js:123:16)
    at new Response (C:\Users\shlomo\projects\tribewise-backend-v1\node_modules\superagent\lib\node\response.js:41:8)
    at Request._emitResponse (C:\Users\shlomo\projects\tribewise-backend-v1\node_modules\superagent\lib\node\index.js:850:20)
    at parser (C:\Users\shlomo\projects\tribewise-backend-v1\node_modules\superagent\lib\node\index.js:1036:38)
    at IncomingMessage.res.on (C:\Users\shlomo\projects\tribewise-backend-v1\node_modules\superagent\lib\node\parsers\json.js:19:7)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickDomainCallback (internal/process/next_tick.js:218:9)
  status: 400,
  text: '{"error":"unsupported_grant_type","error_description":"Grant Type is NULL"}',
  method: 'POST',
  path: '/v1/oauth2/token/' }

为什么我收到错误 unssupported grant type - the grant type is NULL? 在 postman 中它可以工作,但我无法让它与 super 代理和 Node 一起工作。 Paypal举了两个例子here与 postman 和 curl 。为什么它不能与 superagent 一起使用?

最佳答案

来自您链接到的 PayPal 文档:

If you use a command-line tool other than cURL, set the Accept header to application/x-www-form-urlencoded

来自您链接到的 superget 文档:

By default sending strings will set the Content-Type to application/x-www-form-urlencoded

因此将您的请求更改为:

// PAYPAL_LINK = "https://api.sandbox.paypal.com"
// PAYPAL_OAUTH_API = "/v1/oauth2/token"
request(PAYPAL_LINK)
  .post(PAYPAL_OAUTH_API)
  .send('grant_type=client_credentials')
  .auth(PAYPAL_CLIENT, PAYPAL_SECRET)
  .then(response => console.log(response))
  .catch(error => console.error(error))

如果您希望响应为json,则可以选择设置接受:application/json

关于node.js - 如何修复 grant_type 错误为 NULL - Pay Pal 的 REST API 不支持访问 token 的授权类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55777438/

相关文章:

node.js - 如何让异步readFile方法遵循node.js中的顺序

javascript - 如何测试命名窗口是否打开或被弹出窗口阻止程序阻止?

paypal - 如何将 paypal 循环 gem 与 Spree 集成

paypal - 如何在新的 paypal View 中启用 IPN?

ios - 如何使用 iOS SDK 从 Facebook 获取长期访问 token ?

rest - 连接 OAuth2 资源服务器和认证服务器

c# - 是否有通用模式来同步从多个线程创建访问 token ?

node.js - express 和 socket.io - 声明和启动服务器

javascript - createWriteStream 的标记选项

Visual Studio 的 Node.js 工具无法调试