Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to emulate a curl request using node-fetch module

i have an e-commerce application and trying to reach out to the paypal rest api, "paypal for partners" service specifically, i did read the Paypal Documentation and its all good but the problem is that they mentioned the request example using curl like this :

curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
   -H "Accept: application/json" \
   -H "Accept-Language: en_US" \
   -u "client_id:secret" \
   -d "grant_type=client_credentials"

Or

using postman with basic Auth:

  • Username: Your client ID.

  • Password: Your secret.

iam trying to implement the same thing but using node-fetch from node.js

const fetch = require('node-fetch');

function authenticatePaypal() {
    fetch('https://api.sandbox.paypal.com/v1/oauth2/token', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Accept-Language': 'en_US',
            'client_id': 'secret'
        },
        body: {
            "grant_type": "client_credentials"
        }
    }).then(reply => {
        console.log('success');
        console.log(reply);
    }).catch(err => {
        console.log('error');
        console.log(err);
    });
}

module.exports = {
    authenticatePaypal: authenticatePaypal
};

and i get this response of 401 Unauthorized:

Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]:
   { body:
      PassThrough {
        _readableState: [ReadableState],
        readable: true,
        _events: [Object],
        _eventsCount: 2,
        _maxListeners: undefined,
        _writableState: [WritableState],
        writable: false,
        allowHalfOpen: true,
        _transformState: [Object] },
     disturbed: false,
     error: null },
  [Symbol(Response internals)]:
   { url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
     status: 401,
     statusText: 'Unauthorized',
     headers: Headers { [Symbol(map)]: [Object] } } }

i tried post man and it worked in postman, i know that there is something wrong in my node-fetch implementation, this is my first time dealing with basic Auth in json format.

like image 684
Mahmoud Fawzy Avatar asked Jan 29 '19 12:01

Mahmoud Fawzy


2 Answers

Authorization header is wrong.

-u "client_id:secret"

says that curl is using a Basic Authentication.

You should add authorization header

Authorization: Basic <base64 encoded "client_id:secret">
like image 97
karoluS Avatar answered Sep 27 '22 18:09

karoluS


Solution using yours as base, since I struggled a few minutes to have it working.

// get the client_id and secret from https://developer.paypal.com/developer/applications/
const clientIdAndSecret = <client_id:secret>
const base64 = Buffer.from(clientIdAndSecret).toString('base64')

fetch('https://api.sandbox.paypal.com/v1/oauth2/token', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept-Language': 'en_US',
        'Accept': 'application/json',
        'Authorization': `Basic ${base64}`,
      },
      body: 'grant_type=client_credentials'
})
like image 40
1911z Avatar answered Sep 27 '22 17:09

1911z