Jigar
Jigar

Reputation: 11

Do i need to have Microsoft 365 subscription to use Graph API for outlook in python?

i am trying to fetch all my mails from outlook using Graph API, and i am stuck in this error

i am using delegated flow for access token i am calling this end point 'https://login.microsoftonline.com/{{TenantID}}/oauth2/v2.0/token' for getting an access token

AuthURl : 'https://login.microsoftonline.com/{{TenantID}}/oauth2/v2.0/authorize'

Access Token URL : 'https://login.microsoftonline.com/common/oauth2/v2.0/token'

i am able to get access token but when i try to call this end point with accesstoken 'https://graph.microsoft.com/v1.0/me/messages' it gives me this error

{"error":{"code":"OrganizationFromTenantGuidNotFound","message":"The tenant for tenant guid
'28331ffe-XXXX-46af-9415-XXXXXXXXXXXX## Heading ##' does not
exist.","innerError":{"oAuthEventOperationId":"84f7039d-34b8-4e17-a66a-2c672f1a1468","oAuthEventcV":"/GzDJG2u4Bnxk14D+fZR+w.1.1","errorUrl":"https://aka.ms/autherrors#error-InvalidTenant","requestId":"ffc2a401-b871-4aa9-b1ea-3ce5739e07c8","date":"2024-06-14T06:29:26"}}}

it works fine in graph explorer but not in API

as far i researched it says i need Microsoft 365 with active subscription.

Upvotes: -1

Views: 208

Answers (1)

Sridevi
Sridevi

Reputation: 22512

The error occurred as you are using "tenant ID" in authorization endpoint while signing in with personal Microsoft accounts. To resolve the error, make use of /common endpoint for both authorization request and token generation.

In my case, I ran below authorization request in browser and got code value successfully after signing in with personal Microsoft account:

https://login.microsoftonline.com/common/oauth2/v2.0/authorize
?client_id=appId
&response_type=code
&redirect_uri=https://jwt.ms
&response_mode=query
&scope=Mail.Read
&state=12345

enter image description here

Now, I used this code to generate access token using authorization code flow via Postman:

POST https://login.microsoftonline.com/common/oauth2/v2.0/token
grant_type:authorization_code
client_id:appId
client_secret:secret
scope:Mail.Read
code:code
redirect_uri: https://jwt.ms

Response:

enter image description here

When I used this token to call API, I got messages successfully like this:

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

Response:

enter image description here

Note that, you will get only 10 messages in API response by default. To get more, make use of $top query parameter in API call to get all messages by modifying the page size based on your requirement:

GET https://graph.microsoft.com/v1.0/me/messages?$top=100

Reference: List messages - Microsoft Graph

Upvotes: 1

Related Questions