1
votes

My company is using Microsoft 365 Business Standard licenses. We are using email through these accounts. We also have a few shared mailboxes. We are trying to create an app that uses the microsoft graph application permissions (rather than the delegated permissions) so the application can access one of the shared mailboxes without needing to be authenticated under the current user.

This is the steps we have taken so far:

  1. Within Microsoft Azure, we have an application in which we have granted application api permissions for Mail.Read, and we have accepted Admin consent.

  2. We authorized as an app, not as a user, in the application using this endpoint https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize, pointing out the required parameters for sending a request. Then, MS API builds this link:

https://login.microsoftonline.com/{some_string}/oauth2/v2.0/authorize?state={some_string}&scope=offline_access%20https%3A%2F%2Fgraph.microsoft.com%2F.default&response_type=code&approval_prompt=auto&redirect_uri=http%3A%2F%2Flocalhost&client_id={some_string}

When we follow the link, we get to the standard authorization form on the site. After we log in, a link is created, where we take the code and create the token: http://localhost/?code={some_string}&state={some_string}&session_state={some_string}

  1. When we try to hit this endpoint: https://graph.microsoft.com/v1.0/users/[email protected]/messages, we get this response:

{ "error": { "code": "ErrorAccessDenied", "message": "Access is denied. Check credentials and try again.", "innerError": { "date": "2020-09-14T11:22:30", "request-id": "{some_string}", "client-request-id": "{some_string}" } } }

I am thinking that hitting this endpoint https://graph.microsoft.com/v1.0/users/[email protected]/messages requires us to pass the token previously generated and/or specify which application is making the query?

Any help or direction on what needs to be done to make this query work would be greatly appreciated. Thank you!

2

2 Answers

0
votes

I am thinking that hitting this endpoint https://graph.microsoft.com/v1.0/users/[email protected]/messages requires us to pass the token previously generated and/or specify which application is making the query?

Yes you would need to send the AccessToken in the Authorization header, you should also include the x-anchormailbox header which helps route the request to correct mailbox eg

GET https://graph.microsoft.com/v1.0/users/[email protected]/messages HTTP/1.1
Host: graph.microsoft.com
Authorization: Bearer EwAoA8l6BAAU ... 7PqHGsykYj7A0XqHCjbKKgWSkcAg==
X-AnchorMailbox: [email protected]

The other thing you might want to check is to ensure you have the correct scopes in your token you can use https://jwt.io/ for that

0
votes

In order to use application permissions you will need to use the client credentials auth flow (not the authorization code auth flow which uses delegated permissions). To get a token make a request against "/oauth2/v2.0/token" and specify "grant_type=client_credentials" in the request. See examples of client credentials auth flow here for more details: https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow