我使用的是 here年最近发布的Google People API的示例.我稍微扩展了一些示例,以显示有关联系人的其他信息,例如电子邮件地址和电话号码.应该完成这项工作的代码如下所示. public class Peop
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->联系人验证).我错过了什么?
好的,问题解决了.看起来谷歌的文档有点误导(好吧,它刚刚发布;)).当我尝试使用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.");
}
}
}
