1

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/xyz@gmail.com/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));
                    }
});
slfan
  • 8,950
  • 115
  • 65
  • 78
Surya Babu
  • 11
  • 1
  • 3
  • `data:{accessToken: authResult }` should be `data: JSON.stringify({accessToken: authResult }, null, ' ')` so that your auth token is encoded properly and you don't get `[object%20Object]`. – coolaj86 Dec 14 '13 at 06:21

3 Answers3

1

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

Ryan Boyd
  • 2,978
  • 1
  • 21
  • 19
1

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

Community
  • 1
  • 1
Azr
  • 1,083
  • 1
  • 13
  • 26
0

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

Uday Reddy
  • 1,337
  • 3
  • 16
  • 38
javiercbk
  • 470
  • 3
  • 11