Mike VanDerwerker
Mike VanDerwerker

Reputation: 13

Office 365 Mail API: Admin access token to access everyone's emails

Is it possible for me to access everyone emails within my company using the Office 365 Mail API? I know I can access individuals, but hat requires me to be granted access to their emails and get their access tokens. I would like to be able to have an admin access token and be able to access everyone's emails. Any ideas whether this is possible or not?

Upvotes: 0

Views: 1263

Answers (1)

Fei Xue
Fei Xue

Reputation: 14649

It is possible. We can register a Web application and/or Web API (default, known as a confidential client in OAuth2 parlance) an build a daemon service to retrieve all the messages for organization.

Here is an example that using the client credentials to request the token and get the messages from different users:

string authority = "https://login.microsoftonline.com/msdnofficedev.onmicrosoft.com";
        string resource = "https://Graph.microsoft.com";
        string clientID = "";
        string clientSecret = "";
        AuthenticationContext ac = new AuthenticationContext(authority);
        AuthenticationResult ar = ac.AcquireToken(resource, new ClientCredential(clientID, clientSecret));

        HttpClient hc = new HttpClient();
        hc.DefaultRequestHeaders.Add("Authorization", "Bearer " + ar.AccessToken);
        HttpResponseMessage hrm = await hc.GetAsync("https://Graph.microsoft.com/v1.0/users/[email protected]/messages/");
        string content=await hrm.Content.ReadAsStringAsync();
        MessageBox.Show(content);

        HttpResponseMessage hrm2 = await hc.GetAsync("https://Graph.microsoft.com/v1.0/users/[email protected]/messages/");
        content = await hrm2.Content.ReadAsStringAsync();
        MessageBox.Show(content);

For more detail about register application in Azure AD, you can follow link below: https://azure.microsoft.com/en-us/documentation/articles/active-directory-integrating-applications/#BKMK_Native

Upvotes: 1

Related Questions