1

I have meet big problem about read mail from my outlook account, it takes me about serval days, however still failed, somebody can help me?

I am referring to this document:

https://learn.microsoft.com/en-us/graph/auth-v2-user?view=graph-rest-1.0&tabs=http

Api premission config

Here is my code:

Step 1: (get code)

https://login.microsoftonline.com/c4acbf1d-ea8c-4147-8bbf-23e6f48ffb3e/oauth2/v2.0/authorize?client_id=2b331fc4-2778-4ada-93c0-33cbbbeed6db&response_type=code&redirect_uri=http://localhost:8888/auth&response_mode=query&scope=offline_access%20User.Read%20Mail.Read%20Mail.ReadBasic&state=test123

code is: 1.AcYAHb-sxIzqR0GLvyPm9I_7PsQfMyt4J9pKk8Azy7vu1tvpAADGAA.AgABBAIAAABVrSpeuWamRam2jAF1XRQEAwDs_wUA9P_7JtLMtdErg-aeUPnOQWCp.......

Step 2: (get token)

Get token by http post

Then i got token and refresh token, scope is correct

Step 3: (test get users)

Get user info http GET

Yes, successfully get user info.

Step 4: (test get user mail messages)

Get user messages fail

Unfortunately, it failed, i don't know why and how to fix it, please help, thanks.

This is my request header: Req header snapshot


While when i use another mode, Get access without a user, still have the same problem, Of course, I used another method to obtain the new token, which is valid because I have already obtained the user's ID. the doc: https://learn.microsoft.com/en-us/graph/auth-v2-service?tabs=http

GET https://graph.microsoft.com/v1.0/users/9dfb85a5-b8bb-4e7d-8e05-a60d418ca16d/messages?$select=sender,subject
Authorization: Bearer ey.....TJxBtlAXdJARLAJP4KcQ9mnA
User-Agent: IntelliJ HTTP Client/IntelliJ IDEA 2024.2.1
Accept-Encoding: br, deflate, gzip, x-gzip
Accept: */*
content-length: 0

Reponse:

HTTP/2 401 Unauthorized
cache-control: private
strict-transport-security: max-age=31536000
request-id: 7e3e5860-1bf0-4d7f-8701-c593b759f488
client-request-id: 7e3e5860-1bf0-4d7f-8701-c593b759f488
x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"Korea     Central","Slice":"E","Ring":"4","ScaleUnit":"003","RoleInstance":"SE1PEPF0001058B"}}
date: Fri, 09 May 2025 06:39:00 GMT
content-length: 0
x-http2-stream-id: 3

<Response body is empty>

Response code: 401 (Unauthorized); Time: 1563ms (1 s 563 ms); Content length: 0 bytes (0 B)
6
  • Are you sure about that you've already set the correct authorization? You'd better show your request header in the tab 实际请求. If the token's expired, it can also cause 401 error. Commented May 9 at 3:38
  • Thanks for your help, i have attach another snapshot for request headers, thanks. Commented May 9 at 3:47
  • you can't read emails of other users with delegated permissions. You need to use application permissions a get access without a user Commented May 9 at 4:59
  • Hello, yes, i have try this mode, however still the same error, i have edit post and paste new information, please help, thanks. Commented May 9 at 6:46
  • As you want to read messages from personal Microsoft account, make sure to create app registration with supported account type as 'Accounts in any organizational directory (any Microsoft Entra ID tenant – multitenant) and personal Microsoft accounts (e.g., Skype, Xbox)' and use /common instead of tenantId to generate token. Commented May 9 at 7:00

1 Answer 1

0

As you want to read messages from personal Microsoft account, make sure to create app registration with supported account type as 'Accounts in any organizational directory (any Microsoft Entra ID tenant – multitenant) and personal Microsoft accounts (e.g., Skype, Xbox)

Initially, I created an app registration with the supported account type set to 'Accounts in any organizational directory (any Microsoft Entra ID tenant – multitenant) and personal Microsoft accounts (e.g., Skype, Xbox)'. I provided the redirect URI as http://localhost:8888/auth, enter image description here

Granted below API permissions:

enter image description here

Make sure to invite your outlook personal Microsoft account into azure

After that, I generated the authorization code using the endpoint below, using 'common' instead of the tenant ID

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?
client_id=<Enter your client ID>
&response_type=code
&redirect_uri=http://localhost:8888/auth
&response_mode=query
&scope=Mail.Read User.Read openid offline_access

enter image description here

After that, you will be prompted to sign in use your personal Microsoft Outlook account

enter image description here

Then after, by using the endpoint below, you can generate an access token to fetch messages from your personal Microsoft Outlook account. Use common instead of tenant ID, and update the parameters in the body such as client_id, scope, code, redirect_uri, grant_type, and client_secret.

https://login.microsoftonline.com/common/oauth2/v2.0/token

enter image description here

To get the messages from a personal Microsoft account, use the endpoint below

https://graph.microsoft.com/v1.0/me/messages

Response: I am able to fetch messages; the screenshot below is for your reference.

enter image description here

And verified messages in my outlook as well

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Wow, great, thanks very much. I have another question. The/common part is not mentioned in the API documentation. How did you find it? Also, if I use this method, how can I update my token? The interface did not return a refresh token, and my program may be a daemon process in the background
Glad to know that it worked, please post a new question with details so that I can help you there
Can you help me solve another same problem? Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.