-->

Google Contacts API with Google JavaScript Client

2020-02-25 07:36发布

问题:

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?

回答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));
});


回答2:

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



回答3:

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



回答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.



回答5:

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.



回答6:

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.



回答7:

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