1

What is the Ajax call that I should make to get gmail contacts using JavaScript? I already have the user OAuth Token which I got because the user signed up to my site using Google.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Darwish Gani
  • 11
  • 1
  • 2
  • Hi, welcome to Stack Overflow. You might not have had quite right search terms, but see if this helps: https://developers.google.com/google-apps/contacts/v3/ – Qantas 94 Heavy Jun 08 '13 at 02:31
  • https://www.google.com/m8/feeds/contacts/{userEmail}/full It seems that I need to make a GET request to this URL, however, I am confused as to how google knows I have a token that authorizes me to get these contacts. – Darwish Gani Jun 08 '13 at 05:47

2 Answers2

5

If you're using OAuth2 through JavaScript, you can use the Google Contacts API, but you'll need to get authorisation by sending the correct scope of permissions to Google when getting the access token, which is https://www.google.com/m8/feeds. (reference)

As you already know how to get the access token, it's as simple as calling the API with the correct query. To get all contacts for your user, it's as simple as making an asynchronous request to the API for the required info. For example, where {userEmail} is the user's email and {accessToken} is your access token, simply make a GET address to the following URI:

https://www.google.com/m8/feeds/contacts/{userEmail}/full?access_token={accessToken}&alt=json

A list of the types of queries you can send and their parameters are available here:

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
  • (Token invalid - AuthSub token has wrong scope) This is the error that I get. My ajax request looks like this: $.ajax({ url: "https://www.google.com/m8/feeds/contacts/" + email +"/full?access_token=" + gmail_access_token + "&alt=json", dataType: "JSONP", success: function(contacts_data){ console.log(contacts_data); } }); – Darwish Gani Jun 08 '13 at 16:12
  • I have googled around but the solutions don't seem to be working. – Darwish Gani Jun 08 '13 at 16:15
  • @DarwishGani: You have the wrong scope (i.e. set of permissions) for your token. If you change the scope of your permissions when you receive your token, then you should be able to use that. – Qantas 94 Heavy Jun 08 '13 at 23:23
  • I activated contacts API in google before getting the Client ID, do I need to activate another API on this page? https://code.google.com/apis/console/#project:552738833011:services – Darwish Gani Jun 09 '13 at 03:28
  • appreciate the help so far. – Darwish Gani Jun 09 '13 at 03:28
2

To get users' contacts using OAuth, first you need to specify the contact's scope in your request. If you're using ChromeExAuth, then you would write:

var oauth = ChromeExOAuth.initBackgroundPage({
  'request_url' : 'https://www.google.com/accounts/OAuthGetRequestToken',
  'authorize_url' : 'https://www.google.com/accounts/OAuthAuthorizeToken',
  'access_url' : 'https://www.google.com/accounts/OAuthGetAccessToken',
  'consumer_key' : 'anonymous',
  'consumer_secret' : 'anonymous',
  'scope' : 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.google.com/m8/feeds/',
  'app_name' : 'MyApp'
});

The scope parameter above lists 3 scopes: the user's email, profile, and contacts (google.com/m8/feeds/contacts)

To get their contacts after the user authorizes the token, you would send a request like this:

var url = "http://www.google.com/m8/feeds/contacts/default/full";
oauth.sendSignedRequest(url, onContacts, {
  'parameters' : {
    'alt' : 'json',
    'max-results' : 99999
  }
});

And the callback for the request could look like this:

function onContacts(text, xhr) {
  contacts = [];
  var data = JSON.parse(text);
  for (var i = 0, entry; entry = data.feed.entry[i]; i++) {
    var contact = {
      'name' : entry['title']['$t'],
      'id' : entry['id']['$t'],
      'emails' : []
    };

    if (entry['gd$email']) {
      var emails = entry['gd$email'];
      for (var j = 0, email; email = emails[j]; j++) {
        contact['emails'].push(email['address']);
      }
    }

    if (!contact['name']) {
      contact['name'] = contact['emails'][0] || "<Unknown>";
    }
    contacts.push(contact);
  }
};

To view the contacts array, you could just print on the console:

console.log(contacts);

You can checkout the Google OAuth tutorial here

Guilherme
  • 721
  • 6
  • 13
  • Perfectly good way. I would suggest using the Google native library (not browser centric) to get authorization and using jqery's `$.get()` method. – Brandon Clark May 11 '15 at 13:15
  • Is there a way.to selectively get contacts for autocomplete UI. When the user types For is should load matching values loaded at runtime – Code Guy Mar 29 '17 at 02:33
  • @CodeGuy Can't say. I haven't used google APIs for this sort of thing in a really long time. One way to do it would be to download the contacts, and filter them using some library like TypeAhead. You can search for a similar question in SO, and if you can't find one, you can ask a new one – Guilherme Apr 02 '17 at 21:20