【发布时间】:2019-09-10 15:29:02
【问题描述】:
我正在调用 Paypal 的 REST API 以获取访问令牌 https://developer.paypal.com/docs/api/overview/#get-an-access-token
我正在使用带有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/' }
为什么我收到错误 unssuported grant type - grant type 为 NULL? 在邮递员中它可以工作,但我无法让它与超级代理和节点一起工作。 Paypal 给出了两个例子here 和 postman 和 curl。为什么它不能与 superagent 一起使用?
【问题讨论】:
标签: node.js paypal access-token paypal-rest-sdk superagent