How to retrieve PayPal REST Api access-token using node

后端 未结 5 639
一向
一向 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: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");
    });
    

提交回复
热议问题