Login with Google using OAuth 2.0, GET userinfo and parse the response

Showing 1-1 of 1 messages
Login with Google using OAuth 2.0, GET userinfo and parse the response Krishna M 4/25/12 11:24 PM
As described in the google's developers page using OAuth 2.0, I created the unique URL for the appropriate client id

format of the URL is :


Users are able to login and are redirected to my redirect URL but the problem is, when I try to use jquery to parse the response its giving a failure rather that of a Success.

I'm using the following javascript in my redirect url page:

    // First, parse the query string
    var acc_token;
    var params = {}, queryString = location.hash.substring(1),
        regex = /([^&=]+)=([^&]*)/g, m;
    while (m = regex.exec(queryString)) {
        acc_token = m[2];
      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')
            type: 'GET',
           datatype : "json",
           success : function(data)
        error : function()

The javascript is working fine, I'm able to get the access token to a variable and then i want to send a GET request to the google server for the URL https://www.googleapis.com/oauth2/v1/userinfo using the jquery ajax, but its going to the error function. when I manually do the URL with access_token request its giving me a json array with all the details of the user. I also tried changing the datatype to jsonp but no use.

if this method is not possible can anyone suggest me how to handle the response for getting userinfo. Please, also try to give me a sample code.