Error Access to XMLHttpRequest at "http"rom origin has been blocked by CORS policy - Graph API -

Iron Contributor

Hi All,
I would like to retrieve list of recent files from a particular document library or site for the logged on user

This is using a content editor on a sharepoint classic site


When i run the code below i get error

Access to XMLHttpRequest at 'https://login.microsoftonline.com//oauth2/v2.0/token/' from origin 'https://tenant.sharepoint.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Using the code below to get an access token and i get the error above

var token;  
$(document).ready(function () {  
    requestToken();  
});  

function requestToken() {  
    var clientId = ""
    var clientSecret = ""
    var tenantID = ""
    var uri = "https://login.microsoftonline.com/"+ tenantID + "/oauth2/v2.0/token"

    $.ajax({  
        "async": true,  
        "crossDomain": true,  
        "url": "https://login.microsoftonline.com/tenantName/oauth2/v2.0/token", // Pass your tenant 
        
        
        "method": "POST",  
        "headers": {  
            "content-type": "application/x-www-form-urlencoded"  
        },  
        "data": {  
            "grant_type": "client_credentials",  
            "client_id ": clientId, //Provide your app id      
            "client_secret": clientSecret, //Provide your secret      
            "scope ": "https://graph.microsoft.com/.default",
            "redirectUri" :  "https://tenantName.sharepoint.com"
        },  
        success: function (response) {  
            console.log(response);  
            token = response.access_token;  
            console.log(token);  
            
        },  
        error: function (error) {  
            console.log(JSON.stringify(error));  
        }  
    })  
}  

 

I have setup the app registrations and also added Redirect URIs for SPA and Web

 

Not sure what else I'm missing

 

Thanks in Advance

1 Reply