Happy Business Starts Here

Generate OAuth Token from Ajax Call

Advanced Tutor

Generate OAuth Token from Ajax Call

Hello all,


I'm trying to generate an Oauth Token with Ajax call from this endpoint:

But I got an error related to CORS Policy. 


Access to XMLHttpRequest at 'https://rest.zuora.com/oauth/token' from origin 'my-site' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.


My request is something like this:

var details = {
    'client_id': 'my-client-id',
    'client_secret': 'my-client-secret',
    'grant_type': 'client_credentials'
    let promise = new Promise((resolve, reject) => {
        var expectedToken;
        var xhr = new XMLHttpRequest();
        xhr.onload = function () {
            if (xhr.status === 200) {
                var jsonData = JSON.parse(this.responseText);
                expectedToken = jsonData.access_token;
            } else {
                reject("Something went Wrong")

        xhr.open("POST", 'https://rest.zuora.com/oauth/token', true);
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
 With the same information, it's working from PostMan, but with Ajax it's not working, can you please help me with this?
Thanks in advance.


Tags (4)