java - 使用 Google People API (Java) 检索有关联系人的信息

标签 java google-api google-api-client google-contacts-api

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

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-> 联系人验证)。我错过了什么?

最佳答案

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

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

关于java - 使用 Google People API (Java) 检索有关联系人的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35604406/

相关文章:

java - WebSphere : How to retrieve a user's credentials from CAS ticket?

java - TDD:如何创建测试初始值的测试

javascript - 如何更改仅访问我的 Google 帐户以准备 OAuth2 OOB 弃用的不和谐机器人?

java - Eclipse Juno 中的规划视角?

java - 为什么将行分割成数组后,for循环后的代码不起作用?

java - 从 GoogleIdToken 获取用户个人资料

google-api - 如何使用 python 向 google hangouts 发送消息?

android - 检查用户是否已经使用 Auth.GoogleSignInApi 登录?

google-app-engine - 替换 oauth2client.crypt 中的 OpenSSL 以与 PyCrypto 一起使用

android - GoogleApiClient 无法连接