How to retrieve PayPal REST Api access-token using node

后端 未结 5 634
一向
一向 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:11

    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);
      }
    })();
    
    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2021-02-10 09:20

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

    0 讨论(0)
  • 2021-02-10 09:29

    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))
    
    0 讨论(0)
  • 2021-02-10 09:34

    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");
    });
    
    0 讨论(0)
提交回复
热议问题