【问题标题】:Retrieve google contact profile picture检索谷歌联系人个人资料图片
【发布时间】:2017-06-22 22:40:26
【问题描述】:

我需要检索 Google 联系人信息,包括联系人的个人资料图片。为此,我使用下面的代码,除了联系人的个人资料图片外,一切正常:我得到的链接无处可去。有没有其他方法可以获取联系人的头像链接?

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

【问题讨论】:

    标签: java google-people-api


    【解决方案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.");
        }
    

    【讨论】:

    • 此解决方案仅返回已添加到联系人中的人员。给我写信的所有其他联系人都没有返回。但照片 URL 返回正确,我可以看到联系人的个人资料图片
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    相关资源
    最近更新 更多