Paypal api oauth
I'm making a PayPal API call in the context of an e-Commerce solution I'm developing in Angular-JS. I was able to get an auth_token back from PayPal using the following code. The two gotchas that caught me up for some time were:
- The ClientId and ClientSecret have to be joined by a colon and base64 encoded (as FinnK says above)
When sending the data as an object
data: {'grant_type': 'client_credentials'}
I was receiving an error that 'grant_type' is a required parameter. After changing this to a string joined with an equals sign I no longer received this error.var basicAuthString = btoa('CLIENTID:SECRET');$http({ method: 'POST', url: 'https://api.sandbox.paypal.com/v1/oauth2/token', headers: { 'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'Basic ' + basicAuthString, }, data: 'grant_type=client_credentials'}).success(function(data){ console.log(data.access_token);}).error(function(error){ console.error(error);});
I hope this was helpful for someone stuck trying to implement this.
Client id and secret need be sent in an Authorization
header.
Client id and secret need be concatenated with a :
separator (e.g. client:secret
), and then encoded in base 64. This is called basic authentication, and what cURL does behind the scene when using the -u
option.
You can pass custom headers to $http.post
in a third parameter. See the method documentation. Example, using $http
directly (and heavily inspired from the documentation):
var req = { method: 'POST', url: 'http://example.com', headers: { 'Authorization': "Basic 12Base346456enc0did" }, data: { grant_type: 'client_credentials' }}$http(req).success(function(){...}).error(function(){...});