0

I'm currently making the integration between Google Calendar and my company's website. But I'd like the save the user's credential, so he just have to authenticate only one time. I build the following URL.

https://accounts.google.com/o/oauth2/v2/auth?client_id=my_client_id&redirect_uri=localhost&state=django_csrf_token&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar&access_type=offline&response_type=code

And calling the following snippet:

xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
xhr.send();

And now. I'm stuck...

1 Answer 1

0

You cannot perform an OAuth call via an XMLHttpRequest. If the user wasn't currently logged into Google how would the user log in via that call? How would the user approve your application's requested permissions? You must actually redirect their browser to that URL.

2
  • Cool, but how can I recover the resulting object from the authentication? As this authentication should return a JSON that contains a refresh_token property. Jan 9, 2017 at 19:09
  • @JoãoEsteves once you redirect the user to that URL the user will be able to approve your application. After they do, Google will direct the user back to the callback URL you specified in the query string. When you get that request on your server you will see the authorization code in the query string parameters. Use that along with your client id and secret to make a POST request to Google to exchange your authorization code for an access token and refresh token.
    – idbehold
    Jan 9, 2017 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.