2017-06-22 51 views
0

我需要檢索谷歌聯繫人信息,包括聯繫人的個人資料圖片。爲此,我使用下面的代碼,除了聯繫人的個人資料圖片外,其他所有內容都可以使用:我所獲得的鏈接無處不在。有沒有其他方法可以獲得聯繫人的個人資料圖片鏈接?檢索谷歌聯繫人資料圖片

GoogleCredential gc = new GoogleCredential(); 
    gc.setAccessToken(accessToken); 
    ContactsService contactsService = new ContactsService("ServiceName"); 

    contactsService.setOAuth2Credentials(gc); 
    URL url = new URL("https://www.google.com/m8/feeds/contacts/default/full/?max-results=10000"); 
    ContactFeed feed = null; 

    try { 
     feed = contactsService.getFeed(url, ContactFeed.class); 
    } catch (ServiceException e) { 
     e.printStackTrace(); 
    } 

    List<SocialContact> contacts = new ArrayList<>(); 

    if (feed != null) { 
     for (ContactEntry entry : feed.getEntries()) { 
      SocialContact contact = new SocialContact(); 

      if (entry.hasName()) { 
       Name name = entry.getName(); 

       if (name.hasFullName()) { 
        if (name.hasGivenName()) { 
         String givenName = name.getGivenName().getValue(); 

         if (name.getGivenName().hasYomi()) { 
          givenName += " (" + name.getGivenName().getYomi() + ")"; 
         } 

         contact.setFirstName(givenName); 

         if (name.hasFamilyName()) { 
          String familyName = name.getFamilyName().getValue(); 
          if (name.getFamilyName().hasYomi()) { 
           familyName += " (" + name.getFamilyName().getYomi() + ")"; 
          } 
          contact.setLastName(familyName); 
         } 
        } 
       } 
      } 

      for (PhoneNumber number : entry.getPhoneNumbers()) { 
       contact.setPhone(number.getPhoneNumber()); 
      } 

      for (Email email : entry.getEmailAddresses()) { 
       contact.setEmail(email.getAddress()); 
      } 

      contact.setProfileImageURL(entry.getContactPhotoLink().getHref()); 
      if(contact.getEmail() != null){ 
       contacts.add(contact); 
      } 
     } 
    } 

回答

1

聽起來好像您使用的是聯繫人API,但也許您應該使用People API來代替。以下代碼從these docs修改爲:

ListConnectionsResponse response = peopleService.people().connections().list("people/me") 
    .setPersonFields("names,emailAddresses,photos") 
    .setPageSize(10000) 
    .execute(); 

List<Person> connections = response.getConnections(); 
    if (connections != null && connections.size() > 0) { 
     for (Person person : connections) { 
      List<Name> names = person.getNames(); 
      if (names != null && names.size() > 0) { 
       System.out.println("Name: " + person.getNames().get(0) 
         .getDisplayName()); 
      } else { 
       System.out.println("No names available for connection."); 
      } 

      List<Photo> photos = person.getPhotos(); 
      if (photos != null && photos.size() > 0){ 
       System.out.println("Photo URL: " + person.getPhotos().get(0).getURL()); 
      } 
     } 
    } else { 
     System.out.println("No connections found."); 
    } 
+0

此解決方案僅返回已添加到聯繫人中的人員。每個寫信給我的聯繫人都不會返回。但照片網址返回正確,我可以看到聯繫人的個人資料圖片 –