How can I convert this paypal curl command to Axios?
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"
I am using vueAxios in vue, hence the "this.".
this.axios.post('https://api.sandbox.paypal.com/v1/oauth2/token',
{'Accept': "application/json", 'Accept-Language': "en_US",
'XXXXXX': 'XXXXX', // my paypal client ID and client secret
'grant_type':'client_credentials'}
)
.then( response => {
console.log("Response is: ", response);
})
.catch( error => {
console.log("Error is :", error);
});
I am getting this error:
Error is : Error: Request failed with status code 401
at createError (createError.js?16d0:16)
at settle (settle.js?db52:18)
at XMLHttpRequest.handleLoad (xhr.js?ec6c:77)
I've also tried this (which seems to be better but I'm still getting a 400 error):
this.axios({
method: 'POST',
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
headers: {
'Accept': 'application/json',
'Accept-Language': 'en_US',
'Content-Type':'application/x-www-form-urlencoded'
},
auth: {
username: '<XXXX My paypal client ID>',
password: '<XXXX My paypal secret>'
} ,
data: {
grant_type: 'client_credentials'
},
})
.then(function(response) {console.log(response);})
.catch(function(response) {console.log(response);});
UPDATE – after some help form the comments I have tried the following code and the paypal has an issue CORS error ( i have installed a npm packages "cors" and the cors error persists (both locally and when deployed)).
This answers my question but, as stated here, it seems like Paypal doesn't allow requests directly from the browser.
this.axios({
withCredentials: true,
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
method: 'post',
headers: {
'Accept': 'application/json',
'Accept-Language': 'en_US',
'Content-Type':'application/x-www-form-urlencoded',
'Access-Control-Allow-Origin': '*',
},
data: { 'grant_type':'client_credentials' },
auth: {
username: 'XXXXXXXX',
password: 'XXXXXXXX'
}
})
Related documentation:
CORS: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
VueAxios: https://www.npmjs.com/package/vue-axios
Paypal dev: https://developer.paypal.com/docs/api/overview/#make-your-first-call