-->

People API of google versus contacts API

2019-04-30 02:40发布

问题:

While trying to fetch contacts using google account of user , I am facing some issues after using people API.It only returns few email addresses out of all listed ones.Access token and all scopes have been set correctly. Code for following :

People peopleService = new People.Builder(httpTransport, jsonFactory, credential)
                    .build();
ListConnectionsResponse response = peopleService.people().connections().list("people/me")
                    .setPageSize(500).setSortOrder("FIRST_NAME_ASCENDING")
                    .setAccessToken(tokenResponse.getAccessToken())
                    .setAlt("json")
                    .setRequestMaskIncludeField("person.names,person.emailAddresses,person.phoneNumbers")
                    . execute();
   connections = response.getConnections();

Instead of this if I use contact API of google then I am getting more no of email addresses than people.Code for contact API :

URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full");
    ContactFeed resultFeed = myService.getFeed(feedUrl, ContactFeed.class);

    // Print the results
    System.out.println(resultFeed.getTitle().getPlainText());
    for (ContactEntry entry : resultFeed.getEntries()) {
          ....
          .....
          .......
 }

I want to know if there is any difference between both of them and which one i have to use for better results or am I missing something. please suggest. Thanks..!!

回答1:

People API is more up-to-date. Reading through Google's blog announcement, People API simplifies what needed to be separate calls to Google+ API and Contacts API. Now you only need to use one.

"The new People API uses the newest protocols and technologies and will eventually replace the Contacts API which uses the GData protocol"

When getting the user's list of connections, be sure to specify the correct scopes when using it.

https://www.googleapis.com/auth/contacts - Requests that your app be given read and write access to the contacts in the authenticated user’s Google Contacts. https://www.googleapis.com/auth/contacts.readonly - Requests that your app be given read access to the contacts in the authenticated user’s Google Contacts.

Check this link for similarities and differences between People API and Contacts API.



回答2:

I'm not sure if there is any issues with the java code, but if you look at the request that is sent out it should be like this:

URL:https://content-people.googleapis.com/v1/people/me/connections?pageSize=300&requestMask.includeField=person.names%2Cperson.email_addresses&sortOrder=FIRST_NAME_ASCENDING&key=

I was able to get all the contacts with the correct email and name. If you google "manage google contacts" you will see a list of contacts. From there take that number, and just print out the connection count which should match. You just need to make sure the pageSize is big enough or you have to handle paging to get all the contacts.

Also i realized that the requestMask.includeField is really sensitive to what parameters and spaces you put. "person.name,person.email_addresses" worked and "person.name, person.email_addresses" did not.

With the older contact api, you can query the q parameter which I don't think the people api provides this ability. I think the ability to filter your search by key words is important to reduce the request size.