Search code examples
paypalaxiospaypal-sandboxpaypal-subscriptions

node.js / axios AUTHENTICATION_FAILURE with PayPal Subscriptions API


I'm using Axios to activate a PayPal subscription since the NODE SDK doesn't support the subscription activation. For doing so I've created this method that generate a PayPal access token:

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

the options contains the following details:

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'
}

this working fine but I'm having some problems activating the subscription, this is the method that handle this:

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;
        });
}

Essentially I pass the generated accessToken and the subscriptionId, but I get as response this:

{
  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'
    }
  ]
}

My suspicion was that the generated token was incorrect, so I tried it in postman sending this request:

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

and the returned content is 204 which is okay according to what doc says here.

The request seems correct, what am I doing wrong?


Solution

  • You're setting the headers wrong. From the 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 },

    So, PayPal isn't actually getting your Authorization header