Retrieving information about a contact with Google People API (Java)
Asked Answered
F

1

6

I am using an example of recently released Google's People API from here. I have extended a sample a bit to display additional information about the contact such as an email address and a phone number. The code that should do the job is presented below.

public class PeopleQuickstart {

    ...

    public static void getPersonInfo(Person person){

        // Get names
        List<Name> names = person.getNames();
        if(names != null && names.size() > 0) {
            for(Name personName: names) {
                System.out.println("Name: " + personName.getDisplayName());
            }
        }

        // Get email addresses
        List<EmailAddress> emails = person.getEmailAddresses();
        if(emails != null && emails.size() > 0) {
            for(EmailAddress personEmail: emails) {
                System.out.println("Email: " + personEmail.getValue());
            }
        }

        // Get phone numbers
        List<PhoneNumber> phones = person.getPhoneNumbers();
        if(phones != null && phones.size() > 0) {
            for(PhoneNumber personPhone: phones){
                System.out.println("Phone number: " + personPhone.getValue());
            }
        }
    }

    public static void main(String [] args)  throws IOException {

        People service = getPeopleService();

        // Request 120 connections.
        ListConnectionsResponse response = service.people().connections()
                .list("people/me")
                .setPageSize(120)
                .execute();

        // Display information about your connections.
        List<Person> connections = response.getConnections();
        if (connections != null && connections.size() > 0) {
            for (Person person: connections){
                getPersonInfo(person);
            }
        } else {
            System.out.println("No connections found.");
        }   
    }
}

I am testing this program with my contact list and I can successfully obtain a list of people along with the name fields. However, I cannot get values for email addresses and phone numbers (lists are always null), although I do have these values set in my contact list (verified through Gmail->Contacts). What am I missing?

Fescennine answered 24/2, 2016 at 14:8 Comment(0)
F
19

Ok, problem solved. It looks like Google's documentation is a bit misleading (well, it has just been released;)). When I try to fetch my contacts using people.connections.list (see here) there are several query parameters that can be set. However, for the requestMask parameter it is stated that "Omitting this field will include all fields" which is not the case (at least did not work for me). Therefore, one has to explicitly specify which fields to be returned in the response. The modified code is given below. I wish Google people would clarify this point a bit.

public class PeopleQuickstart {

    ...

    public static void main(String [] args)  throws IOException {

        People service = getPeopleService();

        // Request 120 connections.
        ListConnectionsResponse response = service.people().connections()
                .list("people/me")
                .setPageSize(120)
                // specify fields to be returned
                .setRequestMaskIncludeField("person.names,person.emailAddresses,person.phoneNumbers")
                .execute();

        // Display information about a person.
        List<Person> connections = response.getConnections();
        if (connections != null && connections.size() > 0) {
            for (Person person: connections){
                getPersonInfo(person);
            }
        } else {
            System.out.println("No connections found.");
        }   
    }
}
Fescennine answered 25/2, 2016 at 9:31 Comment(7)
For posterity, here is the list of valid request masks: person.addresses, person.age_range, person.biographies, person.birthdays, person.bragging_rights, person.cover_photos, person.email_addresses, person.events, person.genders, person.im_clients, person.interests, person.locales, person.memberships, person.metadata, person.names, person.nicknames, person.occupations, person.organizations, person.phone_numbers, person.photos, person.relations, person.relationship_interests, person.relationship_statuses, person.residences, person.skills, person.taglines, person.urlsKippar
Had the same problem (#36466550). Glad you found the solution. We should report this to Google.Minica
can't access emailAddress, others are working fine. Can anyone help?Stigmatize
Now mask parameters are - person.addresses, person.age_ranges, person.biographies, person.birthdays, person.bragging_rights, person.cover_photos, person.email_addresses, person.events, person.genders, person.im_clients, person.interests, person.locales, person.memberships, person.metadata, person.names, person.nicknames, person.occupations, person.organizations, person.phone_numbers, person.photos, person.relations, person.relationship_interests, person.relationship_statuses, person.residences, person.skills, person.taglines, person.urlsStigmatize
thanks for post answer. Its working perfect and really helpful.Secular
how about google id? is it inside any tag u provided?Antipersonnel
How to get Data in JSON FormatNuli

© 2022 - 2024 — McMap. All rights reserved.