Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I cant do a POST request to paypal API #653

Closed
Spartano opened this issue Jan 15, 2017 · 3 comments
Closed

I cant do a POST request to paypal API #653

Spartano opened this issue Jan 15, 2017 · 3 comments

Comments

@Spartano
Copy link

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.

@rubennorte
Copy link
Member

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:

@ttiagojm
Copy link

ttiagojm commented Aug 20, 2018

@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);});

@ttiagojm
Copy link

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

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants