6

How to get the PayPal access-token needed to leverage the REST Api by using node?

matteo
  • 1,635
  • 1
  • 15
  • 26

5 Answers5

18

Once you have a PayPal client Id and a Client Secret you can use the following:

var request = require('request');

request.post({
    uri: "https://api.sandbox.paypal.com/v1/oauth2/token",
    headers: {
        "Accept": "application/json",
        "Accept-Language": "en_US",
        "content-type": "application/x-www-form-urlencoded"
    },
    auth: {
    'user': '---your cliend ID---',
    'pass': '---your client secret---',
    // 'sendImmediately': false
  },
  form: {
    "grant_type": "client_credentials"
  }
}, function(error, response, body) {
    console.log(body);
});

The response, if successful, will be something as the following:

{
    "scope":"https://api.paypal.com/v1/payments/.* ---and more URL callable with the access-token---",
    "access_token":"---your access-token---",
    "token_type":"Bearer",
    "app_id":"APP-1234567890",
    "expires_in":28800
}
matteo
  • 1,635
  • 1
  • 15
  • 26
  • 1
    Thanks a lot for your reply! How can I get hold of "access_token", I've tried like this "body.access_token" but it returns undefined. – Dimitri Oct 07 '17 at 10:37
  • 1
    @Dimitri `const { access_token } = JSON.parse(body);` I would we translate this into native fetch() ? – Aamir Afridi Mar 25 '19 at 22:40
15

Also, you can use axios, and async/await:

const axios = require('axios');

(async () => {
  try {
    const { data: { access_token } } = await axios({
      url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
      method: 'post',
      headers: {
        Accept: 'application/json',
        'Accept-Language': 'en_US',
        'content-type': 'application/x-www-form-urlencoded',
      },
      auth: {
        username: client_id,
        password: client_secret,
      },
      params: {
        grant_type: 'client_credentials',
      },
    });

    console.log('access_token: ', access_token);
  } catch (e) {
    console.error(e);
  }
})();
Yurii Holskyi
  • 878
  • 1
  • 13
  • 28
5

Modern problems require modern solutions:

const fetch = require('node-fetch');
const authUrl = "https://api-m.sandbox.paypal.com/v1/oauth2/token";
const clientIdAndSecret = "CLIENT_ID:SECRET_CODE";
const base64 = Buffer.from(clientIdAndSecret).toString('base64')

fetch(authUrl, { 
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'Accept-Language': 'en_US',
        'Authorization': `Basic ${base64}`,
    },
    body: 'grant_type=client_credentials'
}).then(function(response) {
    return response.json();
}).then(function(data) {
    console.log(data.access_token);
}).catch(function() {
    console.log("couldn't get auth token");
});
Dustin Spengler
  • 5,478
  • 4
  • 28
  • 36
  • You are a LIFE saver. I've been struggling with PayPal/React and sandbox subscription testing for days... I can get it working in prod, but not sandbox and this was the first step to getting that working – a_lovelace Feb 17 '21 at 18:15
1

You could use PayPal-Node-SDK to make calls to PayPal Rest APIs. It handles all the authorization and authentication for you.

Jay Patel - PayPal
  • 1,489
  • 1
  • 11
  • 20
  • What you say it's true for most of the needs but not for "payment-experience/web-profiles" for example. – matteo Feb 09 '15 at 17:14
  • 1
    Again, not a nodejs engineer, however, I see that PayPal-Node-SDK has samples too for https://github.com/paypal/PayPal-node-SDK/tree/master/samples/payment_experience/web_profile – Jay Patel - PayPal Feb 09 '15 at 17:19
  • Let me know if you still found an issue using node-sdk. We would love to help get things fixed/updated to let get you to integrate paypal apis faster. – Jay Patel - PayPal Feb 21 '15 at 06:57
0

Here is how I get the access_token using superagent

        superagent.post('https://api.sandbox.paypal.com/v1/oauth2/token')
        .set("Accept","application/json")
        .set("Accept-Language","en_US")
        .set("content-type","application/x-www-form-urlencoded")
        .auth("Your Client Id","Your Secret")
        .send({"grant_type": "client_credentials"})
        .then((res) => console.log("response",res.body))
Craig Howard
  • 1,649
  • 15
  • 10