2016-02-24 14 views
5

Tôi đang sử dụng ví dụ về API người của Google được phát hành gần đây từ here. Tôi đã mở rộng một mẫu để hiển thị thông tin bổ sung về liên hệ như địa chỉ email và số điện thoại. Mã nên thực hiện công việc được trình bày bên dưới.Truy xuất thông tin về một liên hệ với API của Google People (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."); 
     } 
    } 
} 

Tôi đang thử nghiệm chương trình này với danh sách liên hệ của mình và tôi có thể có được danh sách người cùng với tên trường thành công. Tuy nhiên, tôi không thể nhận được giá trị cho địa chỉ email và số điện thoại (danh sách luôn rỗng), mặc dù tôi có các giá trị này được đặt trong danh sách liên hệ của mình (được xác minh qua Gmail-> Danh bạ). Tôi đang thiếu gì?

Trả lời

16

Ok, đã giải quyết được sự cố. Dường như tài liệu của Google có chút sai lệch (tốt, nó vừa được phát hành;)). Khi tôi cố tìm nạp các liên hệ của mình bằng cách sử dụng people.connections.list (xem here) có một số tham số truy vấn có thể được đặt. Tuy nhiên, đối với tham số requestMask, thông báo rằng "Bỏ qua trường này sẽ bao gồm tất cả các trường" không phải trường hợp này (ít nhất là không hoạt động đối với tôi). Do đó, người ta phải xác định rõ các trường nào được trả về trong phản hồi. Mã được sửa đổi được đưa ra dưới đây. Tôi muốn mọi người của Google sẽ làm rõ điều này một chút.

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

Đối với hậu thế, đây là danh sách các mặt nạ yêu cầu hợp lệ: 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. mối quan hệ, person.relationship_interests, person.relationship_statuses, person.residences, person.skills, person.taglines, person.urls – GBleaney

+0

Có cùng một vấn đề (http://stackoverflow.com/questions/36466050/why-cant-i-retrieve -emails-addresses-and-phone-numbers-với-google-people-api). Rất vui khi bạn tìm thấy giải pháp. Chúng tôi nên báo cáo điều này cho Google. – nunoarruda

+0

@foma cảm ơn, nó giúp tôi tiết kiệm. – Ankur1994a

Các vấn đề liên quan