Working with Contacts Using the Google People Service Client in Java
PeopleClient from the com.aspose.email.services.google.people.client package works with contacts and contact groups through the Google People API. The client connects over HTTP. To learn how to select an HTTP library, see HTTP Clients for Mail Services.
Connecting to Google People
The client authenticates with OAuth 2.0 in the same way as GmailClient. For details, see Connecting to Gmail.
The following code sample creates a client and prints the profile of the authenticated user. Other code samples on this page use the people variable created here.
import com.aspose.email.services.google.base.auth.GoogleOAuthTokenProvider;
import com.aspose.email.services.google.people.client.PeopleClient;
import com.aspose.email.services.google.people.model.GooglePerson;
GoogleOAuthTokenProvider tokens = new GoogleOAuthTokenProvider("client-id", "client-secret", "refresh-token");
try (PeopleClient people = PeopleClient.builder().tokenProvider(tokens).build()) {
GooglePerson me = people.me().get();
System.out.println(me.getDisplayName() + ": " + me.getPrimaryEmail());
}
Listing Contacts
The listAll method reads all pages of contacts and returns them as one list:
import com.aspose.email.services.google.people.model.GooglePerson;
for (GooglePerson person : people.contacts().listAll()) {
System.out.println(person.getResourceName() + ": " + person.getDisplayName() + ", " + person.getPrimaryEmail());
}
By default, the client requests names, email addresses, phone numbers, addresses and group memberships. To request other fields or to read one page at a time, use list with the names of the fields, the size of the page and the page token:
import com.aspose.email.services.google.base.api.GooglePagedResult;
import com.aspose.email.services.google.people.model.GooglePerson;
String pageToken = null;
do {
GooglePagedResult<GooglePerson> page = people.contacts().list("names,emailAddresses", 100, pageToken);
for (GooglePerson person : page.getItems()) {
System.out.println(person.getDisplayName());
}
pageToken = page.getNextPageToken();
} while (pageToken != null);
Creating a Contact
Create a GooglePerson object, set its properties, and then pass it to create. The constructors of GoogleEmailAddress and GooglePhoneNumber accept the value and its type. The returned object contains the resource name of the new contact.
import com.aspose.email.services.google.people.model.GoogleEmailAddress;
import com.aspose.email.services.google.people.model.GoogleName;
import com.aspose.email.services.google.people.model.GooglePerson;
import com.aspose.email.services.google.people.model.GooglePhoneNumber;
import java.util.Arrays;
GooglePerson person = new GooglePerson();
person.setNames(Arrays.asList(new GoogleName("Olena", "Shevchenko")));
person.setEmailAddresses(Arrays.asList(new GoogleEmailAddress("olena@example.com", GoogleEmailAddress.TYPE_WORK)));
person.setPhoneNumbers(Arrays.asList(new GooglePhoneNumber("+380441234567", GooglePhoneNumber.TYPE_MOBILE)));
GooglePerson created = people.contacts().create(person);
System.out.println(created.getResourceName());
Reading and Deleting a Contact
To work with one contact, call people with its resource name:
import com.aspose.email.services.google.people.model.GooglePerson;
GooglePerson person = people.people("people/c1234567890").get();
System.out.println(person.getDisplayName());
people.people("people/c1234567890").delete();
Working with Contact Groups
The contactGroups method returns a client for listing and creating groups. To work with one group, pass its resource name. The delete method accepts a parameter that specifies whether to delete the contacts of the group as well.
import com.aspose.email.services.google.people.model.GoogleContactGroup;
for (GoogleContactGroup group : people.contactGroups().listUserGroups()) {
System.out.println(group.getResourceName() + ": " + group.getName() + ", " + group.getMemberCount());
}
GoogleContactGroup group = new GoogleContactGroup();
group.setName("Partners");
GoogleContactGroup created = people.contactGroups().create(group);
people.contactGroups(created.getResourceName()).delete(false);
Handling Errors
When the Google People API returns an error status, the client throws HttpClientException, which contains the status and the response body. If the token provider cannot get an access token, the client throws java.io.IOException, which is also the base class of HttpClientException.
import com.aspose.ext.http.HttpClientException;
try {
people.people("people/unknown-id").get();
} catch (HttpClientException e) {
System.out.println(e.getStatusCode() + ": " + e.getResponseBody());
}