node.js / axios AUTHENTICATION_FAILURE 与 PayPal 订阅 API

node.js / axios AUTHENTICATION_FAILURE with PayPal Subscriptions API

我正在使用 Axios 激活 PayPal 订阅,因为 NODE SDK 不支持订阅激活。为此,我创建了这个生成 PayPal 访问令牌的方法:

let getAccessToken = async () => {
    return await axios(options).then((response) => {
        return response.data.access_token
    });
}

选项包含以下详细信息:

const options = {
    method: 'post',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Access-Control-Allow-Credentials': true
    },
    data: qs.stringify(data),
    auth: {
        username: PAYPAL_CLIENT_ID,
        password: PAYPAL_CLIENT_SECRET
    },
    url: 'https://api.sandbox.paypal.com/v1/oauth2/token'
}

这工作正常,但我在激活订阅时遇到了一些问题,这是处理此问题的方法:

let activateSubscription = async (accessToken, subscriptionId) => {

    return await axios.post(
        `${baseURL}/v1/billing/subscriptions/${subscriptionId}/activate`,
        {
            headers: {
                "Authorization": `Bearer ${accessToken}`,
                "Content-Type": 'application/json'
            }
        }).then((data) => {
            return true;
        })
        .catch((error) => {
            console.log(error.response.data);
            return false;
        });
}

基本上我传递了生成的 accessTokensubscriptionId,但我得到的响应是:

{
  name: 'AUTHENTICATION_FAILURE',
  message: 'Authentication failed due to invalid authentication credentials or a missing Authorization header.',
  links: [
    {
      href: 'https://developer.paypal.com/docs/api/overview/#error',
      rel: 'information_link'
    }
  ]
}

我怀疑是生成的token不正确,所以我在邮递员发送这个请求时试了一下:

{{host}}/v1/billing/subscriptions/I-7D10FGKVNMD0/activate

并且返回的内容是 204 根据文档所说 here.

没问题

请求似乎是正确的,我做错了什么?

您设置的 header 有误。来自 pastebin:

data: '{"headers":{"Authorization":"Bearer A21AAEj_0lJjny7Hc1aL7l5irIxOqOjyW_pSfT2WC9APAQFXHTYzKL0womW1mZvS6mKWsWMytMc6H6NIMPMnOK7zhzHKHsSAw","Content-Type":"application/json"}}', headers: { Accept: 'application/json, text/plain, */*', 'Content-Type': 'application/json;charset=utf-8', 'User-Agent': 'axios/0.19.2', 'Content-Length': 170 },

因此,PayPal 实际上并未获得您的授权 header