Shivam Sinha
Shivam Sinha

Reputation: 5150

Google Contacts API 401 Authorization Required

I am making the following request to the Google Contacts API after obtaining a oauth2 token.

$.ajax({
         url: 'https://www.google.com/m8/feeds/contacts/[email protected]/full?access_token=' + authData.authorizationCode + '&alt=json',
         dataType: "jsonp",
         success:function(data) {
                    console.log((data));
                }
            });

This produces the following GET HTTP request:

https://www.google.com/m8/feeds/contacts/[email protected]/full?access_token=4/95N2f8xA1XXXXXXXXXXXXXXQC3BtuuMbk.Aug-7c4GLLMZXmXvfARQvtgU5IVtmgI&alt=json&callback=jQuery111XXXXX1426XXX03&_=1431142848004

However google responds back with Status Code:401 Authorization required

Upvotes: 3

Views: 1815

Answers (1)

Shivam Sinha
Shivam Sinha

Reputation: 5150

FYI I was using the authorization code and not the access token to request for the contact details. Hence why I was getting the error

Diagram here illustrates the oauth2 flow: https://developers.google.com/identity/protocols/OAuth2WebServer

This is a great resource for seeing what your code should actually be doing: https://developers.google.com/oauthplayground/

Here is a comprehensive example of using googles oauth2 for api's: http://www.9bitstudios.com/2013/05/using-oauth-2-0-for-google-apis/

Below code gets the list of emails contacts

$.ajax({
                    url: 'https://www.google.com/m8/feeds/contacts/default/full?alt=json',
                    dataType: "jsonp",
                    data: {'access_token': authData.authorizationToken.access_token},
                    success:function(data) {
                        var entryOfEmails = data.feed.entry;
                        for (var i = 0; i < data.feed.entry.length; i++) {
                                console.log(entryOfEmails[i].gd$email[0].address);
                            }
                    }
                 });  

Upvotes: 3

Related Questions