How to use GraphClient for Microsoft Graph

Working with GraphClient

An instance of the IGraphClient class handles building requests, sending them to the Microsoft Graph API, and processing the responses.

Create an ITokenProvider Object

To create a new instance of IGraphClient class, you need to provide an instance of ITokenProvider, which can authenticate requests to Microsoft Graph.

ITokenProvider tokenProvider = new ITokenProvider() {
    Date expirationDate = null;

    @Override
    public void dispose() {
    }

    @Override
    public OAuthToken getAccessToken(boolean ignoreExistingToken) {
        // Gets oAuth access token.
        // If ignoreExistingToken is true, requests new token from a server. Otherwise behavior is depended on whether token exists or not.
        // If token exists and its expiration date is not expired returns current token, otherwise requests new token from a server.
        return null;
    }

    @Override
    public OAuthToken getAccessToken() {
        // Gets oAuth access token.
        // If token exists and its expiration date is not expired returns current token, otherwise requests new token from a server.
        return new OAuthToken("token", expirationDate);
    }
};

Get a GraphClient object

After you have set the TokenProvider, you must get a IGraphClient object to make requests against the service. After you have a IGraphClient that is authenticated, you can begin making calls against the service.

IGraphClient client = GraphClient.getClient(tokenProvider);

Connecting to GCC High Endpoints

The IGraphClient supports connecting to GCC High endpoints through the setEndpoint method. The following code sample demonstrates how to configure the client to connect to the GCC High endpoint for listing folders and retrieving messages:

client.setEndpoint("https://graph.microsoft.us");

GraphFolderInfoCollection folders = client.listFolders();

String folderId = null;
for (GraphFolderInfo folder : folders) {
    if ("Inbox".equals(folder.getDisplayName())) {
        folderId = folder.getItemId();
        break;
    }
}

GraphMessageInfoCollection messages = client.listMessages(folderId);
System.out.println("Messages in the Inbox: " + messages.size());

Work with Folders using Microsoft Graph

Listing, creating, reading, updating, copying, moving and deleting mailbox folders is described in Manage Folders with Microsoft Graph.

List Messages using Microsoft Graph

Listing, fetching, paging, creating, updating, sending, copying, moving and deleting messages, and managing their attachments, is described in Manage Messages with Microsoft Graph.

Categories Api

See How to Work with Outlook Categories.

Overrides Api

See How to Work with Overrides.

Rules Api

See How to Work with Outlook Rules.

Using Resource to Support Multiple Mailboxes

See Working with Multiple Mailboxes - Switching Mailbox Context.

Notebooks Api

To manage OneNote notebooks with Microsoft Graph, Aspose.Email for Java offers the following methods of IGraphClient:

  • createNotebook(GraphNotebook notebook)
  • copyNotebook(String notebookId, String groupId, String renameAs)
  • fetchNotebook(String notebookId)
  • listNotebooks() and listNotebooks(ODataQueryBuilder query)
// Create a OneNote notebook
GraphNotebook newNotebook = new GraphNotebook();
newNotebook.setDisplayName("My Notebook");

GraphNotebook createdNotebook = client.createNotebook(newNotebook);

// Fetch a notebook
GraphNotebook fetchedNotebook = client.fetchNotebook(createdNotebook.getId());
System.out.println(fetchedNotebook.getDisplayName());

// List the notebooks
GraphNotebookCollection notebooks = client.listNotebooks();
for (GraphNotebook notebook : notebooks) {
    System.out.println(notebook.getDisplayName());
}