I attempted to access my OAuth2 Token through an XHR-Request in TypeScript, but I consistently receive a 400 error with the message: {"error":"unsupported_grant_type","error_description":"Grant Type is NULL"}
The request is being made from a NativeScript Application (using a TypeScript Template) running on an iOS emulator.
Here is my code:
var oReq = new XMLHttpRequest();
oReq.open("POST", "https://api.sandbox.paypal.com/v1/oauth2/token", true);
// oReq.withCredentials = true;
// oReq.setRequestHeader('grant_type', "client_credentials");
// where client_credentials = clientID:secret
oReq.setRequestHeader('Authorization', "Basic " + this.authorizationBasic);
//where AutorizationBasic is the base64 String of my credentials
oReq.setRequestHeader('Accept', "application/json");
oReq.setRequestHeader('Accept-Language', "en_US");
oReq.onreadystatechange = function () {
console.log("state changed - new state: " + oReq.readyState + " and Status: " + oReq.status);
if (oReq.readyState === 4) {
if (oReq.status === 200) {
console.log(oReq.responseText);
} else {
console.log("Error: " + oReq.status + " Message: " + oReq.responseText);
}
}
};
console.log("Status: " + oReq.status);
oReq.send();
Upon researching, it appears that others have faced similar issues with the PayPal API, as discussed here. Has anyone managed to resolve this problem? Could there be an issue with my code? I have tried various approaches, as seen in the commented lines above, without success.
Check out the PayPal API documentation here: https://developer.paypal.com/docs/integration/direct/make-your-first-call/
I am growing increasingly frustrated as this simple API call is taking longer than expected. The curl command with my clientID and secret worked for me. Does anyone have any suggestions on how to proceed? Is there a more straightforward method than using XHR?