Axios: I cant do a POST request to paypal API

Created on 15 Jan 2017  ·  3Comments  ·  Source: axios/axios

Hi, mi issue is more of a question, i am tryng to make axios do a POST request that does the same thing that this curl does:

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

my code is this:

    var instance = axios.create({
        baseURL: 'https://api.sandbox.paypal.com/v1/oauth2/',
        timeout: 1000,
        method: 'post',
        headers: {
          'Accept': 'application/json',
          'Accept-Language': 'en_US',
          'content-type': 'application/x-www-form-urlencoded',
          'EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp':'EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp'
        },
        data: {
          "grant_type": "client_credentials",
        },    
});

  instance.post('/token')
      .then(function(response){
        console.log(response.data); // ex.: { user: 'Your User'}
        console.log(response.status); // ex.: 200
      });

i am calling it from my localhost, and it gives me the error of 401. This is the link to the PAYPAL API https://developer.paypal.com/docs/integration/direct/make-your-first-call/

Reanding the docs i wasnt able to understand where to put the username and passowrd or how to set up the autorization to basicOuth
instance.defaults.headers.common['Authorization'] = AUTH_TOKEN;
what is AUTH_TOKEN? is a const? what value should i give it?

Anyway, thank you in advance.

Most helpful comment

I solved it! I made a Gist to show how I solved it if someone needs help: https://gist.github.com/TigaxMT/b57c11df3e4ebd0510858ec3df775efa

All 3 comments

You need to authenticate with username and password using basic authentication. Also you're sending JSON with that request and the curl version uses "application/x-www-form-urlencoded ". Please read the documentation:

@Spartano You solved the problem ? I'm trying to do the same thing and give me a 401 too and this message "Authentication failed due to invalid authentication credentials or a missing Authorization header."

My code here:

axios.post('https://api.sandbox.paypal.com/v1/oauth2/token', querystring.stringify({

    grant_type: 'client_credentials',

    withCredentials: true,

    headers:{

        'Accept': 'application/json',
        'Accept-Language': 'en_US',
        'Content-Type': 'application/x-www-form-urlencoded',
    },

    auth: {

        username: 'ClientID',
                password: 'Secret'
    }


})).then((response) => {

    console.log(response);

}).catch((err) => {console.log(err);});

I solved it! I made a Gist to show how I solved it if someone needs help: https://gist.github.com/TigaxMT/b57c11df3e4ebd0510858ec3df775efa

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ildella picture ildella  ·  3Comments

Adman picture Adman  ·  3Comments

c0debreaker picture c0debreaker  ·  3Comments

altruisticsoftware picture altruisticsoftware  ·  3Comments

airtonix picture airtonix  ·  3Comments