Surya Babu
Surya Babu

Reputation: 11

Retrieving google contacts using oauth 2.0

I have been wrestling here to get the google contacts, I get the user personal information, but unable to get the user's contacts. getting following error: 401 (Authorization required)

https://www.google.com/m8/feeds/contacts/[email protected]/full?oauthtoken=[object%20Object]&callback=jQuery162xxxxxxxxxx_13xxxxxxxxxx2&_=13xxxxxx.

$.ajax({
                    url: "https://www.google.com/m8/feeds/contacts/default/full",
                    dataType: "jsonp",
                    headers: "GData-Version: 3.0",
                    data:{accessToken: authResult },
                    success: function (data) {
                        console.log("json"+JSON.stringify(data));
                    }
});

Upvotes: 1

Views: 3725

Answers (3)

Azr
Azr

Reputation: 1143

I answered this :

// OAuth configurations   
    var config = {
      'client_id': 'xxxxxx.apps.googleusercontent.com',
      'scope': 'https://www.google.com/m8/feeds/contacts/default/full'          
    };

gapi.auth.authorize(config, function(data) {
      // login complete - now get token
      var token = gapi.auth.getToken();
      token.alt = 'json';
      // retrieve contacts
      jQuery.ajax({
        url: 'https://www.google.com/m8/feeds/contacts/default/full/?max-results=999999',
        dataType: 'jsonp',
        data: token,
        success: function(data) { successGmail(data); }
      });
    });

There : Modify HTTP Headers for a JSONP request

Upvotes: 1

javiercbk
javiercbk

Reputation: 470

You can try to write your token in your header as "Authorization: Bearer " + authResult so I thing your code should be the following for this example:

$.ajax({
       url: "https://www.google.com/m8/feeds/contacts/default/full",
       type: "GET",
       dataType: "jsonp",
       headers: {
                   "Authorization": "Bearer " + authResult,
                   "GData-Version": "3.0"
                },
       success: function (data) {
            console.log("json"+JSON.stringify(data));
       }
});

JQuery Docs says the following about ajax headers parameter:

A map of additional header key/value pairs to send along with the request[...]

So that's why I've changed your original string to a map. Type is by default GET so it's optional.

Hope this helps

Upvotes: 0

Ryan Boyd
Ryan Boyd

Reputation: 3018

In your first code block (with the URL), you specify the query parameter name oauthtoken. In the second code block, you specify a parameter value of accessToken.

This parameter value should actually be access_token. See here: https://developers.google.com/accounts/docs/OAuth2UserAgent#callinganapi

Upvotes: 1

Related Questions