Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 694
matteo Avatar asked Feb 06 '15 08:02

matteo


1 Answers

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
}
like image 180
matteo Avatar answered Nov 07 '22 21:11

matteo