In this exercise you will incorporate the Microsoft Graph into the application. For this application, you will use the Microsoft Graph SDK for Java to make calls to Microsoft Graph.
The Microsoft Graph SDK for Java requires an implementation of the IAuthenticationProvider interface to instantiate its GraphServiceClient object.
-
Create a new file in the ./graphtutorial/src/main/java/graphtutorial directory named SimpleAuthProvider.java and add the following code.
:::code language="java" source="../demo/graphtutorial/src/main/java/graphtutorial/SimpleAuthProvider.java" id="AuthProviderSnippet":::
-
Create a new file in the ./graphtutorial/src/main/java/graphtutorial directory named Graph.java and add the following code.
package graphtutorial; import java.util.LinkedList; import java.util.List; import com.microsoft.graph.logger.DefaultLogger; import com.microsoft.graph.logger.LoggerLevel; import com.microsoft.graph.models.extensions.Event; import com.microsoft.graph.models.extensions.IGraphServiceClient; import com.microsoft.graph.models.extensions.User; import com.microsoft.graph.options.Option; import com.microsoft.graph.options.QueryOption; import com.microsoft.graph.requests.extensions.GraphServiceClient; import com.microsoft.graph.requests.extensions.IEventCollectionPage; /** * Graph */ public class Graph { private static IGraphServiceClient graphClient = null; private static SimpleAuthProvider authProvider = null; private static void ensureGraphClient(String accessToken) { if (graphClient == null) { // Create the auth provider authProvider = new SimpleAuthProvider(accessToken); // Create default logger to only log errors DefaultLogger logger = new DefaultLogger(); logger.setLoggingLevel(LoggerLevel.ERROR); // Build a Graph client graphClient = GraphServiceClient.builder() .authenticationProvider(authProvider) .logger(logger) .buildClient(); } } public static User getUser(String accessToken) { ensureGraphClient(accessToken); // GET /me to get authenticated user User me = graphClient .me() .buildRequest() .get(); return me; } }
-
Add the following
importstatement at the top of App.java.import com.microsoft.graph.models.extensions.User;
-
Add the following code in App.java just before the
Scanner input = new Scanner(System.in);line to get the user and output the user's display name.// Greet the user User user = Graph.getUser(accessToken); System.out.println("Welcome " + user.displayName); System.out.println();
-
Run the app. After you log in the app welcomes you by name.
-
Add the following function to the
Graphclass in Graph.java to get events from the user's calendar.:::code language="java" source="../demo/graphtutorial/src/main/java/graphtutorial/Graph.java" id="GetEventsSnippet":::
Consider what this code is doing.
- The URL that will be called is
/me/events. - The
selectfunction limits the fields returned for each event to just those the app will actually use. - A
QueryOptionis used to sort the results by the date and time they were created, with the most recent item being first.
-
Add the following
importstatements in App.java.import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; import java.util.List; import com.microsoft.graph.models.extensions.DateTimeTimeZone; import com.microsoft.graph.models.extensions.Event;
-
Add the following function to the
Appclass to format the dateTimeTimeZone properties from Microsoft Graph into a user-friendly format.:::code language="java" source="../demo/graphtutorial/src/main/java/graphtutorial/App.java" id="FormatDateSnippet":::
-
Add the following function to the
Appclass to get the user's events and output them to the console.:::code language="java" source="../demo/graphtutorial/src/main/java/graphtutorial/App.java" id="ListEventsSnippet":::
-
Add the following just after the
// List the calendarcomment in themainfunction.listCalendarEvents(accessToken);
-
Save all of your changes, build the app, then run it. Choose the List calendar events option to see a list of the user's events.
Welcome Adele Vance Please choose one of the following options: 0. Exit 1. Display access token 2. List calendar events 2 Events: Subject: Team meeting Organizer: Adele Vance Start: 5/22/19, 3:00 PM (UTC) End: 5/22/19, 4:00 PM (UTC) Subject: Team Lunch Organizer: Adele Vance Start: 5/24/19, 6:30 PM (UTC) End: 5/24/19, 8:00 PM (UTC) Subject: Flight to Redmond Organizer: Adele Vance Start: 5/26/19, 4:30 PM (UTC) End: 5/26/19, 7:00 PM (UTC) Subject: Let's meet to discuss strategy Organizer: Patti Fernandez Start: 5/27/19, 10:00 PM (UTC) End: 5/27/19, 10:30 PM (UTC) Subject: All-hands meeting Organizer: Adele Vance Start: 5/28/19, 3:30 PM (UTC) End: 5/28/19, 5:00 PM (UTC)