0
votes

I'm trying to get the information of an outlook.com user mailbox and subsequently the detailed information of each message but I'm having problems with the generated Token. It's giving me a ResourceNotFound code when I try to get the message list. These are the steps that I've taken in order to generate the token.

1.- Microsoft Azure

I've registered a new application in Azure Active Directory. I've added the user that I want the info from as a Guest user, generated a Client Secret, added this URI (https://localhost/myapp/) as a Redirect URI and activated the Mail.Read permission (Both Delegated and Application) among others.

Azure Permissions

2.- Token Generation

Using the Ids from the application created the previous step I proceeded to generate a code using this URL in my browser and logging in to the Outlook account: https://login.microsoftonline.com/{TenantId}/oauth2/v2.0/authorize?client_id={ClientId}&response_type=code&redirect_uri=https%3A%2F%2Flocalhost%2Fmyapp%2F&scope=offline_access%20mail.read&response_mode=query&state=12345

Once I got the code, I made a POST request via Postman to https://login.microsoftonline.com/{TenantId}/oauth2/v2.0/token with a form-data body containing the required parameters according to the documentation, one of them being the code in question.

With that I'm able to succesfully generate an Access Token and a Refresh Token. Now, when I use this Token for a GET request to the endpoint https://graph.microsoft.com/v1.0/me/ I can retrieve the data of the account.

/me/ Info GET

But when I use it for a request to the endpoint https://graph.microsoft.com/v1.0/me/messages I get the error code ResourceNotFound.

Error Code

Is there something I'm missing? I'm following the documentation provided in this link: https://docs.microsoft.com/en-us/graph/auth-v2-user. Thanks in advance to everyone that can help.

1
Can you try replacing {TenantId} in your authorize URL with common or consumers and see if that helps? This is assuming you set your app registration to allow users from any organization and personal accounts.Jason Johnston
@JasonJohnston thanks a lot! By changing the 'TenantId' for 'common' I generated a token which was able to retrieve the message list and information of each message of the outlook email. I'd like to mark your answer as as the solution. This might be a bit off topic but do you know if there is a limit on refresh tokens? I'd like to have constant access to the inbox without having to generate a code everytime.Diego Del Castillo
In general, as long as you are refreshing tokens regularly (which gives both a new access token and a new refresh token), you should be able to maintain access unless the user revokes it. By default, if you do not use a refresh token in 90 days, it expires.Jason Johnston
That behavior can be modified by tenant admins.Jason Johnston
@JasonJohnston I see. Thanks for all the help!Diego Del Castillo

1 Answers

0
votes

The comment made by Jason Johnston had the answer. By replacing {TenantId} for 'common' I was able to generate the correct token to get the information I needed. Thanks!