使用 Google People API (Java) 检索有关联系人的信息 [英] Retrieving information about a contact with Google People API (Java)

查看:18
本文介绍了使用 Google People API (Java) 检索有关联系人的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 此处 中最近发布的 Google People API 示例.我扩展了一个示例以显示有关联系人的其他信息,例如电子邮件地址和电话号码.应该完成这项工作的代码如下所示.

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.");
        }   
    }
}

我正在使用我的联系人列表测试此程序,并且可以成功获取人员列表以及姓名字段.但是,我无法获取电子邮件地址和电话号码的值(列表始终为空),尽管我在我的联系人列表中设置了这些值(通过 Gmail-> 联系人验证).我错过了什么?

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?

推荐答案

好的,问题解决了.看起来谷歌的文档有点误导(好吧,它刚刚发布;)).当我尝试使用 people.connections.list 获取我的联系人时(请参阅 这里) 有几个可以设置的查询参数.但是,对于 requestMask 参数,它声明省略此字段将包括所有字段",事实并非如此(至少对我不起作用).因此,必须明确指定要在响应中返回哪些字段.修改后的代码如下.我希望谷歌人能澄清这一点.

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.");
        }   
    }
}

这篇关于使用 Google People API (Java) 检索有关联系人的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆