matteo
matteo

Reputation: 1725

How to retrieve PayPal REST Api access-token using node

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

Upvotes: 7

Views: 9353

Answers (5)

Yurii Holskyi
Yurii Holskyi

Reputation: 928

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

Upvotes: 17

Dustin Spengler
Dustin Spengler

Reputation: 7741

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

Upvotes: 7

Craig Howard
Craig Howard

Reputation: 1689

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

Upvotes: 0

Jay Patel - PayPal
Jay Patel - PayPal

Reputation: 1489

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

Upvotes: 1

matteo
matteo

Reputation: 1725

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
}

Upvotes: 18

Related Questions