Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 394
foma Avatar asked Feb 24 '16 14:02

foma


1 Answers

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.");
        }   
    }
}
like image 192
foma Avatar answered Oct 24 '22 04:10

foma