skip to Main Content

I have an API for get token from paypal.

curl -v POST 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"

I am using axios to make a call, something like this but I got the error on authentication.

axios.post("https://api.sandbox.paypal.com/v1/oauth2/token", {}, {
      auth: {
        username: "xxxx, // clientId
        password: "xxxx"  // client Secret
      }
    }).then(function(response) {
      result = response;
      console.log('Authenticated' + response);
    }).catch(function(error) {
      console.log('Error on Authentication' + error);
    });

May be I missed something like "grant_type"… How to pass these parameter in this call. Please you correct it? Thanks you so much

3

Answers


  1. ${clientId}:${secretKey} needs to be base64 encoded

    In JavaScript you can use btoa(), toString(‘base64’), or with axios set the auth key.

    With curl you can use the -u command line parameter.


    For general rest API usage, when passing a basic auth that way to the oauth2/token endpoint, your query string should be grant_type=client_credentials, as documented here. This will return the access_token that you can use in all other API calls.

    The ‘Connect with PayPal’ documentation you linked to is for that specific integration, which you need an authorization code, but no other API uses that.

    Login or Signup to reply.
  2. axios({
         method: 'POST',
         url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
         headers: {
           'Authorization': `Basic ${clientId}:${secretKey}`,
           'Content-type': 'application/json'
         },
      data: `grant_type=authorization_code&code=${authorizationCode}`
    })
    
    Login or Signup to reply.
  3. Client ID and Client Secret cannot be your username and password. Well -u flag in curl is being used for username and password but here in axios this thing might be different. For Curl knowledge follow this.
    Everything has some alternative if you are struggling a lot then you can go for this lib.
    PayPal-node-SDK. Or Follow some tutorial in youtube.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search