2016-02-24 51 views
5

我使用的是最近發佈的Google人員API here的示例。我已經擴展了一些樣本來顯示有關聯繫人的其他信息,例如電子郵件地址和電話號碼。下面介紹應該完成這項工作的代碼。檢索有關與Google People API(Java)聯繫的信息

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->聯繫人進行了驗證)。我錯過了什麼?

回答

16

好的,問題解決了。 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."); 
     } 
    } 
} 
+5

對於後人,這裏是有效的請求掩碼列表: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。關係,person.relationship_interests,person.relationship_statuses,person.residences,person.skills,person.taglines,person.urls – GBleaney

+0

有同樣的問題(http://stackoverflow.com/questions/36466050/why-cant-i-retrieve -emails,地址和電話號碼與 - 谷歌 - 人-API)。很高興你找到了解決方案。我們應該向Google報告。 – nunoarruda

+0

@foma謝謝,它救了我。 – Ankur1994a