Google Contacts API with Google JavaScript Client Lib

12,376

Solution 1

To use the v3 contacts api with the authentication token provided by gapi (Google JS client lib) this one is helpful, using alt=&json

$.getJSON('https://www.google.com/m8/feeds/contacts/default/full/?access_token=' + 
             authResult.access_token + "&alt=json&callback=?", function(result){
      console.log(JSON.stringify(result));
});

Solution 2

I know it's an old question but this question shows up when looking on how to read the contacts information from a Google account.

If you only need to access the contact to import it or show the email, phone numbers, or other information and you don't need to modify it, you can use the People API (https://developers.google.com/people/). For javascript you can check the samples tab.

I created a gist, which is almost the sample from Google but adding the requestField parameters.

https://gist.github.com/ddbb1977/7a4b408ed17c7249252a

Solution 3

The problem you are encountering is that the Contacts API v3 is an older API that works with the deprecated GData Client Library. Therefore it is incompatible with the newer Google APIs JavaScript Client.

For now you will need to load and use the GData Client library. For further information on the difference between the GData library and the Google APIs client, please refer to this recent SO question: gapi.client.load versus google.load

Solution 4

Unfortunate Google Contacts API does not work with the new Javascript Client Library. It only works with GData Client Library. I tried working GData Client Library, but it is difficult as you get warnings in the documentation at every juncture that the library has been deprecated.

Therefore, I used a hydrid,

  1. using the new Client Library, to get an authentication.
  2. Use a URL to get the contacts

Unfortunately, because of cross domain restrictions you need to use JSONP, otherwise the browser fails.

 <script src="https://apis.google.com/js/client.js"></script>
.....
function contactsInit() {
  var clientId = 'YOURCLIENTID.apps.googleusercontent.com';
  var scopes = 'https://www.google.com/m8/feeds';
  gapi.auth.authorize({
    client_id: clientId, scope: scopes, immediate: false}, 
     handleAuthResult);

 function handleAuthResult(authResult) {
 if (authResult && !authResult.error) {
   var url = 
    "https://www.google.com/m8/feeds/contacts/default/" + 
    "full?alt=json-in-script&access_token=" + 
    authResult.access_token + 
    "&max-results=7000&v=3.0";

   var myJSONP = new Request.JSONP({
      url: url,
      callbackKey: 'jsoncallback',
      data: {
            },
      onRequest: function(url){
                // a script tag is created with a src equal to url
          },
      onComplete: function(data){
                // the request was completed.
            }
      }).send();
    }
 }
}

function Skeleton() {}
  if (!gdata) {
    var gdata = new Skeleton();
    gdata.io  = new Skeleton();
    gdata.io.handleScriptLoaded = function(data)    {
      processContacts(data);
  }
}

Notes: I use Mootools for JSONP but you could also use jquery or vanilla js with How to make a JSONP request from Javascript without JQuery?

You need to provide your own YOURCLIENTID, and define the processContacts function.

The gdata.io.handleScriptLoaded(data) is necessary since this what the url expects during callback.

I use a limit of 7000, but I don't think it is necessary.

If you don't want to use JSONP you could forward the access_token to your webserver, and process the URL there, e.g. with cURL or with Node.js just replace json-in-script with json.

In json-in-script is important on a browser since otherwise the browser croaks.

Thanks to the other answers on this page, that pointed me in the right direction.

I hope that Google will make the Contacts API capable with the new Javascript Client Library. I hope that others will other be able to use this solution in the meantime.

Share:
12,376
einsA
Author by

einsA

Updated on June 03, 2022

Comments

  • einsA
    einsA about 2 years

    I am trying to work with the Google Contacts API v3.

    Because of the OAuth2 authentication and authorization I'm started with the Google APIs Client Library for JavaScript. I have no problems with that part of the API access.

    But after doing the auth part I don't know what to do next. Can I use the google-api-javascript-client for the Google Contacts API v3? In the list of the supported Google APIs by the javascript-client the contacts API does not appear. But I have full access with the OAuth 2.0 Playground tool.

    I am really confused which client is working best with the latest Google Contacts API. What is about the gdata-javascript-client?

    • sufyan.shoaib
      sufyan.shoaib almost 11 years
      any update to this? This seems like the problem still exists, i am unable to use (or may be i dont know), google contacts api v3. has anyone found a solution yet?
  • einsA
    einsA over 11 years
    Thanks for your clarification! I am trying to work with the GData Client Lib. Sadly the authentification doesn't work. I think this is a general problem as the interactive example from Google also didn't work.. Hopefully this is a temporary problem.
  • Ricardo Gomes
    Ricardo Gomes over 10 years
    note: this is subject to cross-origin policy restrictions
  • Ryan
    Ryan about 10 years
    So there is no way to do this with the javascript client lib? gapi.client.load('contacts','v3',function(){}); for example?
  • Natus Drew
    Natus Drew over 9 years
    awesome, this saved my day
  • Wouter Willems
    Wouter Willems about 9 years
    I ran into the same problem, I solved it by first retrieving an access token, and then call the API directly. This is because the javascript api (gapi) does not support retrieving google contacts. Since it was quite the hassle, I wrote a blogpost about it here: labs.magnet.me/nerds/2015/05/11/…
  • gumuruh
    gumuruh almost 5 years
    what about if my single Web page that has a number to be sent to google contact.... what is the preferred (yet simple) way?