Skip to content

Instantly share code, notes, and snippets.

@codemilli
Last active February 14, 2017 08:20
Show Gist options
  • Save codemilli/495bc1962296df5a54cd3d61baa1669e to your computer and use it in GitHub Desktop.
Save codemilli/495bc1962296df5a54cd3d61baa1669e to your computer and use it in GitHub Desktop.
//Below is a JavaScript snippet that parses the response and returns the parameters to the server.
// First, parse the query string
var params = {}, queryString = location.hash.substring(1),
regex = /([^&=]+)=([^&]*)/g, m;
while (m = regex.exec(queryString)) {
params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}
// And send the token over to the server
var req = new XMLHttpRequest();
// consider using POST so query isn't logged
req.open('GET', 'https://' + window.location.host + '/catchtoken?' + queryString, true);
req.onreadystatechange = function (e) {
if (req.readyState == 4) {
if(req.status == 200){
window.location = params['state']
}
else if(req.status == 400) {
alert('There was an error processing the token.')
}
else {
alert('something else other than 200 was returned')
}
}
};
req.send(null);
//This code sends the parameters received on the fragment to the server using XMLHttpRequest. If your application requires other JavaScript code to make calls to a Google API, you can store the access token in the browser's local storage. Also note that this code sends the parameters to the /catchtoken endpoint, and they are sent over an HTTPS channel.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment