O.Ol
O.Ol

Reputation: 1

Retrieve google contact profile picture

I need to retrieve google contacts information including contact's profile picture. To do this I use code below and everything works except contact's profile picture: the link that I get leads to nowhere. Is there any other way to get contact's profile picture link?

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

Upvotes: 0

Views: 182

Answers (1)

BrettJ
BrettJ

Reputation: 6841

It sounds like you are using the Contacts API but maybe you should use the People API instead. The code below is modified from 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.");
    }

Upvotes: 1

Related Questions