How to get Microsoft Graph API token to read mails

Onur Baskin

I'm trying to retrieve mails from my organization's mailbox, and I can do that via Graph Explorer. However, when I use the same information that I used in Graph Explorer, the generated token returns an error stating '/me request is only valid with delegated authentication flow.' in me/messages endpoint.

So, how can I generate the acceptable token for the /me endpoint?

An example python code or example Postman request would be amazing.

scottwtang

It sounds like the endpoint you're using in Graph Explorer is something like this

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

/me is referring to the user signed into Graph Explorer. If you want to read another user's messages you would use

https://graph.microsoft.com/v1.0/users/[email protected]/messages

When connecting to Graph API as an application with no user interaction, you can never use /me endpoints, as there's no user logged in.

Reference

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

Python example to list messages

import requests

def get_messages(access_token, user):

    request_url = f"https://graph.microsoft.com/v1.0/users/{user}/messages"

    request_headers = {
        "Authorization": "Bearer " + access_token
    }

    result = requests.get(url = request_url, headers = request_headers)
    
    return(result)

msgs = get_messages(access_token = token['access_token'], user = "[email protected]")

print(msgs.content)

Additional example of obtaining a token, using an app registration and client secret

import msal

def get_token_with_client_secret(client_id, client_secret, tenant_id):
    # This function is to obtain a bearer token using the client credentials flow, with a client secret instead of a certificate
    # https://docs.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=CS#client-credentials-provider
    
    app = msal.ConfidentialClientApplication(
        client_id         = client_id,
        client_credential = client_secret,
        authority         = f"https://login.microsoftonline.com/{tenant_id}")

    scopes = ["https://graph.microsoft.com/.default"]

    token = app.acquire_token_for_client(scopes = scopes)

    return(token)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get microsoft graph refresh token java sdk?

How do I create an auth token with the new microsoft graph api?

Obtaining a valid access token for Microsoft Graph API

Microsoft Graph API not returning refresh token

How to get Microsoft Graph API Access token from Node Script?

How to refresh a token for Microsoft Graph

Get Token for Microsoft Graph API in Angular2

Get Access Token from Microsoft Graph API using PHP

Microsoft Graph API: Omit user access token

Microsoft Graph API token validation failure

Get token for Microsoft Graph API in Java

Access token for Microsoft Graph API is immediately expired

Microsoft Graph - Get Access Token

microsoft-graph api : Get new access token from refresh token in graph without redirect url

How to Get a valid access token for my API and Microsoft Graph from Azure Active Directory?

Microsoft Graph API - how to get access token without Authorization Code?

How can I get a token for Microsoft Graph to read an excel table?

How to verify oauth token generated using Microsoft Graph API

How to get AppKey using Microsoft Graph API

How do I read data from Microsoft Graph API into PowerBI?

Microsoft O365 REST Graph API does not get all mails

Microsoft Graph API - cannot refresh access token

Microsoft graph api - no refresh_token

How to get Microsoft Graph API Access token using ajax call

Get Token call to Microsoft Graph REST Api gives 400 error

How to set the access token lifetime for an app using the Microsoft Graph API

How to add the permissions in the access token of Microsoft Graph API

Bad Request "ErrorExecuteSearchStaleData" in Microsoft Graph API - Search for Mails

Microsoft GRAPH API to get total number of mails in my inbox