Google and Microsoft Contacts with Java
Learn how to manage Google & Microsoft contacts with the Nylas Java SDK. Build your contacts book integration in 15 minutes.
Java has been one of the most popular programming languages for many years now because of its ability to run on nearly any platform, gigantic selection of libraries, and high level of reliability. It’s no wonder that it’s an extremely popular language for enterprise applications, and its use in Android development has further cemented it as a major player in the software development industry.
The Nylas Contacts API makes it very easy to create and modify contacts for your user’s Google and Microsoft accounts.
This tutorial explains how to use the Nylas Java SDK and Contacts API to create and modify Google and Microsoft contacts. It covers the following functionality
- Set up your Nylas developer account and get your API keys
- Install the Nylas Java SDK
- Read Google and Microsoft Contact Data
- Create and Modify Google and Microsoft Contacts
- Explore the Nylas Contacts API
Prerequisites
Before you can start using the Nylas Java SDK, make sure you have done the following:
- Sign up for your developer account.
- Get your developer keys. You need to have your:
CLIENT_ID
- The CLIENT ID found on the dashboard page for your Nylas App.CLIENT_SECRET
- The CLIENT SECRET found on the dashboard page for your Nylas App.ACCESS_TOKEN
- The access token provided when you authenticate an account to your Nylas App.
Install the Nylas Java SDK
The Nylas Java SDK requires Java 8 or above.
For the following examples, replace X.X.X
with the version you want to use. Take a look at the list of releases to learn about the versions that are available.
Setup with Gradle
If you're using Gradle, add the following to your dependencies section of build.gradle
:
implementation("com.nylas.sdk:nylas-java-sdk:X.X.X")
Setup with Maven
For projects using Maven, add the following to your POM file:
<dependency>
<groupId>com.nylas.sdk</groupId>
<artifactId>nylas-java-sdk</artifactId>
<version>X.X.X</version>
</dependency>
Configure the API Client
At its core, the Nylas Communication Platform is an API client that interfaces with all of the major email, calendar, and contacts providers. First, import the NylasClient
class and create a new instance of this class. Then, import the Account
class and create an instance of it, passing the access token you gathered when you got your developer API keys. In the following example, replace ACCESS_TOKEN
with the appropriate value.
import java.io.IOException;
import com.nylas.RequestFailedException;
import com.nylas.NylasClient;
import com.nylas.Account;
public class ApiClient {
public static void main(String[] args) throws IOException, RequestFailedException {
NylasClient nylas = new NylasClient();
Account account = nylas.account("ACCESS_TOKEN");
}
}
Be Careful with Secrets!
Follow best practices to include secrets like this in your code. A more secure way to provide these values is to use a KeyStore to protect sensitive information.
List Contacts
The next example demonstrates how to display the first and last name, email address, and ID for the first 10 contacts returned from an account. The first step is to get a list of contact objects. The following example uses account.contacts.list()
and passes it a ContactQuery()
to return the first 10 contacts from the user account.
List<Contact> contacts = account.contacts().list(new ContactQuery().limit(10));
Next, we’ll iterate through the list of contact objects and return their first and last name, the first email address stored for the contact, and the contact id
, which can be used to make modifications to the contact.
contacts.stream().forEach((Contact contact) -> {
System.out.printf(
"Name: %s %s | Email %s | ID: %s\n",
contact.getGivenName(),
contact.getSurname(),
contact.getEmails().get(0).getEmail(),
contact.getId());
});
Take a look at the API reference for the contacts endpoint to learn more about the attributes the contact object contains.
Here is the entire code example to list contacts for a user account.
import java.io.IOException;
import java.util.List;
import com.nylas.NylasAccount;
import com.nylas.NylasClient;
import com.nylas.RequestFailedException;
import com.nylas.Contact;
import com.nylas.ContactQuery;
public class ReadContacts {
public static void main(String[] args) throws IOException, RequestFailedException {
// Create client object and connect it to Nylas using
// an account's access token
NylasClient client = new NylasClient();
// Provide the access token for a specific account
NylasAccount account = client.account("ACCESS_TOKEN");
List<Contact> contacts = account.contacts().list(new ContactQuery().limit(10));
contacts.stream().forEach((Contact contact) -> {
System.out.printf(
"Name: %s %s | Email %s | ID: %s\n",
contact.getGivenName(),
contact.getSurname(),
contact.getEmails().get(0).getEmail(),
contact.getId());
});
}
}
Create a Contact
The first step to creating a Google or Microsoft contact with Nylas is to create a new instance of the Contact
class.
Contact contact = new Contact();
Now, define some attributes for the contact.
contact.setGivenName("My Nylas Friend");
contact.setEmails(Arrays.asList(new Contact.Email("work", "[email protected]")));
contact.setNotes("Email, Calendar, and Contacts APIs");
contact.setPhoneNumbers(Arrays.asList(new Contact.PhoneNumber("business", "555-555-5555")));
contact.setWebPages(Arrays.asList(new Contact.WebPage("homepage", "https://nylas.com")));
There is quite a bit happening here, so let’s break it all down.
contact.setGivenName()
defines the first name for the contact,.setMiddleName()
and.setSurname()
can also be used to set the middle and last namescontact.setEmails()
adds an email address to the contact and sets the type towork
. You can also specifypersonal
as a type.- Similarly,
contact.setPhoneNumbers()
attaches a phone number to the contact using the typebusiness
; Google and Microsoft label this as the contact’s work phone number. This label must be one ofbusiness
,organization_main
,mobile
,assistant
,business_fax
,home_fax
,radio
,car
,home
, orpager
. - Finally,
contact.web_pages
connects a website URL to the contact with the labelhomepage
. This label must be one ofhomepage
,profile
,work
, orblog
.
Take a look a the API reference for the contact creation endpoint to learn more about the attributes you can assign to a contact object.
The very last thing you need to do is call account.contacts().create()
, passing the contact we created earlier. This saves the contact to Nylas, which then syncs the new contact to the third-party provider.
contact = account.contacts().create(contact);
Once the contact has been saved and synced to Google, you should see a contact that looks something like this:
Here is an example of the same contact in Microsoft Outlook.
For convenience, here is the entire code example for creating a contact.
import java.io.IOException;
import com.nylas.NylasAccount;
import com.nylas.NylasClient;
import com.nylas.RequestFailedException;
import com.nylas.Contact;
import java.util.Arrays;
public class CreateContact {
public static void main(String[] args) throws IOException, RequestFailedException {
// Create client object and connect it to Nylas using
// an account's access token
NylasClient client = new NylasClient();
// Provide the access token for a specific account
NylasAccount account = client.account("ACCESS_TOKEN");
Contact contact = new Contact();
// The contact's given name is typically their first name,
// you can specify a last name with 'surname'
contact.setGivenName("My Nylas Friend");
// Email address 'type' must be either 'work' or 'personal'
contact.setEmails(Arrays.asList(new Contact.Email("work", "[email protected]")));
contact.setNotes("Email, Calendar, and Contacts APIs");
// Phone number type must be one of 'business', 'organization_main',
// 'mobile', 'assistant', 'business_fax', 'home_fax', 'radio', 'car', 'home', or 'pager'
// Google labels 'business' phone numbers as the contact's Work phone number
contact.setPhoneNumbers(Arrays.asList(new Contact.PhoneNumber("business", "555-555-5555")));
// Web page type must be one of 'homepage', 'profile', 'work', or 'blog'
contact.setWebPages(Arrays.asList(new Contact.WebPage("homepage", "https://nylas.com")));
// Save the contact to Nylas and the 3rd party provider
contact = account.contacts().create(contact);
System.out.println(contact);
}
}
Explore the Nylas Contacts API
If you’ve made it to this point, congratulations! You’ve learned how to create and modify Google and Microsoft contacts with the Nylas Contacts API! 🎉 There is plenty more that you can do with it; take a look at the following resources to learn more about the Nylas Communications Platform capabilities.
- Learn about some of the more important features of the Nylas Contacts API.
- Take a look at additional code examples for the Java SDK to see what else you can do with email, calendars, and contacts.
- Read about how the Nylas Communications Platform works.
- Review The Basics to learn about what it takes to integrate the Nylas Email API into your app.
- Read the blog post Working With the Nylas Contacts API and Java.