Google Contacts API with Google JavaScript Client Lib

时光毁灭记忆、已成空白 提交于 2019-12-03 02:01:21

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));
});
Brendan

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

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

MLU

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.

For fetching list of contacts using Google plus use this :-

<script src="https://apis.google.com/js/client.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
  function auth() {
    var config = {
      'client_id': 'OAUTH_CLIENT_ID',
      'scope': 'https://www.google.com/m8/feeds'
    };
    gapi.auth.authorize(config, function() {
      fetch(gapi.auth.getToken());
    });
  }

  function fetch(token) {
    $.ajax({
    url: "https://www.google.com/m8/feeds/contacts/default/full?access_token=" + token.access_token + "&alt=json",
    dataType: "jsonp",
    success:function(data) {
              console.log(JSON.stringify(data));
    }
});

}

In the HTML Body :-

<button onclick="auth();">GET CONTACTS FEED</button>

The output will have as a field with the contact containing the phone number.

Make sure to get the client id from google developer console with proper redirect uri.

This is what we found to work to get individual data:

var data = (JSON.stringify(data.feed.entry[0].gd$email, null, 4));
console.log(data);

If you run JSON.stringify(data) you can see all of the headers that you can call on.

google contact v3 is your best friend for this job

here you can find all posible request endpoint https://developers.google.com/google-apps/contacts/v3/

like for all contact list this is the end point https://developers.google.com/google-apps/contacts/v3/#retrieving_all_contacts

After get authentication you can do get request to this url to get all contact list this is an example in python http://code.google.com/p/gdata-python-client/source/browse/samples/contacts/contacts_example.py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!