I'm using microsoft graph api to read emails from outlook, the api is working fine when reading emails from certain accoounts but isn't reading emails from the accounts in other domain(gmail)

SANTOSH PANDEY 0 Reputation points
2024-07-17T21:56:33.3166667+00:00

i'm using msal to generate authentication token for the application and the following:
authotity: 'https://login.microsoftonline.com/{tenant_id}'

endpoint: https://graph.microsoft.com/v1.0/users/{user_id}/messages

params = { '$filter': f"receivedDateTime ge 2024-05-18T06:40:16Z and from/emailAddress/address eq '{SENDER_EMAIL}'", '$top': num, # Adjust the number of messages to fetch as needed '$select': 'from,subject,receivedDateTime,bodyPreview,body', '$orderby': 'receivedDateTime desc',}

if subject is not None: params['$filter'] = params['$filter'] +
f" and startsWith(subject,'{subject}')" headers = { 'Authorization': f'Bearer {self.access_token}', 'Content-Type': 'application/json', 'Prefer': 'IdType="ImmutableId"', } response = requests.get(endpoint, headers=headers, params=params,)

Outlook
Outlook
A family of Microsoft email and calendar products.
3,903 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,120 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yakun Huang-MSFT 5,415 Reputation points Microsoft Vendor
    2024-07-18T02:15:03.12+00:00

    Hi @SANTOSH PANDEY

    According to the documentation, there are only two situations where you can access other users' mail. However, the scope of shared mailboxes is limited to people within the organization and cannot be shared for accounts in other domains, so if you need to access mail from other domain users, you need to use application permissions.

    User's image

    More details can be found at this link:

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

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.