SasiKiran SasiKiran - 3 months ago 91
reST (reStructuredText) Question

using refresh token to get access token office 365 rest api

While using the refresh token to get an access token through office 365 REST api I made the following Jquery ajax request.

jQuery.ajax({
url: "https://outlook.office365.com/common/oauth2/token",
type: "post",
headers:{
"Content-Type":"application/x-www-form-urlencoded"
},
data: {
grant_type: "refresh_token",
refresh_token: access_data['refresh_token'],
client_id: consumer_key,
client_secret: consumer_secret,
resource: "https://outlook.office365.com"
},
success: function(response){
console.log(response)
}
})


I get the following error

XMLHttpRequest cannot load
https://outlook.office365.com/common/oauth2/token.
No 'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://localhost' is therefore not allowed access.
The response had HTTP status code 404.


But I was able to make the same POST request in python through
requests
library using same
refresh_token
and
client credentials
and am unable to figure out why the same POST request does not work through
jQuery
. Any help on this issue is appreciated.

Thanks

Answer

The http://outlook.office365.com resource supports CORS, so you shouldn't be running into this problem. I found this question on Stack Overflow and the accepted answer suggests that jQuery alters the request so that CORS doesn't work and that you need to edit the request headers before executing it.

Make sure your jQuery request has the correct header. Access-Control-Allow-Headers: x-requested-with