How to retrieve PayPal REST Api access-token using node

后端 未结 5 635
一向
一向 2021-02-10 09:03

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

5条回答
  •  不知归路
    2021-02-10 09:15

    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
    }
    

提交回复
热议问题