Как исправить ошибку grant_type: NULL - неподдерживаемый тип гранта с REST API Pay Pal для маркера доступа - PullRequest
1 голос
/ 20 апреля 2019

Я вызываю REST API Paypal для получения токена доступа https://developer.paypal.com/docs/api/overview/#get-an-access-token

Я использую узел с суперагентом для вызова 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/' }

Почему я получаю ошибку неподтвержденного типа предоставления - тип предоставления равен NULL?В почтальоне это работает, но я не могу заставить его работать с суперагентом и узлом.Paypal приводит два примера здесь с почтальоном и curl.Почему он не работает с суперагентом?

1 Ответ

1 голос
/ 20 апреля 2019

Из документов PayPal, с которыми вы связались:

Если вы используете инструмент командной строки, отличный от cURL, установите Accept header на application/x-www-form-urlencoded

Из superget документов, с которыми вы связались:

По умолчанию при отправке строк для Content-Type устанавливается 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))

Опционально установить Accept: application/json, если вы хотите получить ответ как json

...