Skip to content

Latest commit

 

History

History
159 lines (119 loc) · 5.74 KB

File metadata and controls

159 lines (119 loc) · 5.74 KB

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.

Implement an authentication provider

The Microsoft Graph SDK for Java requires an implementation of the IAuthenticationProvider interface to instantiate its GraphServiceClient object.

  1. 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":::

Get user details

  1. 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;
        }
    }
  2. Add the following import statement at the top of App.java.

    import com.microsoft.graph.models.extensions.User;
  3. 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();
  4. Run the app. After you log in the app welcomes you by name.

Get calendar events from Outlook

  1. Add the following function to the Graph class 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 select function limits the fields returned for each event to just those the app will actually use.
  • A QueryOption is used to sort the results by the date and time they were created, with the most recent item being first.

Display the results

  1. Add the following import statements 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;
  2. Add the following function to the App class 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":::

  3. Add the following function to the App class 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":::

  4. Add the following just after the // List the calendar comment in the main function.

    listCalendarEvents(accessToken);
  5. 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)