I'm working with paypal API and flowing with this document make your firest call.
I got a Status Code 400 (Bad Request) with an error message:
Object {error: "invalid_request", error_description: "grant_type is a required parameter"}
'Authorization': "Basic RU9KMlMtWjZPb05fbGVfS1MxZDc1d3NaNnkwU0ZkVnNZOTE4M0l2eEZ5WnA6RUNsdXNNRVVrOGU5aWhJN1pkVkxGNWNaNnkwU0ZkVnNZOTE4M0l2eEZ5WnA=",
In this case I used the Client-Id and Secret which provided by the example and encoded them in base 64.
$http({
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': "Basic RU9KMlMtWjZPb05fbGVfS1MxZDc1d3NaNnkwU0ZkVnNZOTE4M0l2eEZ5WnA6RUNsdXNNRVVrOGU5aWhJN1pkVkxGNWNaNnkwU0ZkVnNZOTE4M0l2eEZ5WnA=",
'Accept-Language': 'en_US'
},
data: { grant_type: 'client_credentials' }
}).success(function (data, status, headers, config) {
console.log(data)
}).error(function (data, status, headers, config) {
console.log(data)
});
The root cause is your data is encoded as JSON instead of
application/x-www-form-urlencoded
, so what you need to do is URL-encode data like:You'll need to include the 'withCredentials' option. Make sure that you put a colon in between your client-id and your secret key. You shouldn't need to encode those values at all, just use the ones Paypal gives you. I also think the data field should not be an object, as Paypal seems to be expecting a string.
If you are not using windows, I believe you'll need to set the content type to JSON, but the documentation is a bit vague there. Maybe try both? I think this request config should work:
Lastly, you may need to configure your $httpProvider. I've never had to, but I'm unsure of your environment and Angular version: