msgraph-sdk-python icon indicating copy to clipboard operation
msgraph-sdk-python copied to clipboard

How to authenticate using a user's raw access token?

Open rau opened this issue 2 years ago • 7 comments

dumb question - but I do the authorization flow, get a user's "oauth access token" and store it. how do i authenticate the msgraphclient using that? doesnt' seem like theres an option anywhere.

essentially a duplicate of this issue: https://github.com/microsoftgraph/msgraph-sdk-java/issues/961

rau avatar Dec 06 '23 20:12 rau

I would also like to know how to do this. Authentication takes place on a different device than the one using it.

dernstOfExtron avatar Feb 12 '24 19:02 dernstOfExtron

Just encountered this issue as well today. I ended up creating a simple custom authentication provider that just returns the raw access token.

from azure.core.credentials import AccessToken

class RawAccessTokenProvider:
    """
    A simple credential provider that returns a raw access token for use with Azure SDK clients.
    """

    def __init__(self, access_token: str, expires_on: int) -> None:
        self._access_token = access_token
        self._expires_on = expires_on

    def get_token(self, *scopes, **kwargs) -> AccessToken:
        return AccessToken(self._access_token, self._expires_on)

# Usage
credentials = RawAccessTokenProvider(raw_access_token, expires_in)
client = GraphServiceClient(credentials=credentials)

me = await client.me.get()

eilonmore avatar Feb 13 '24 11:02 eilonmore

@rau Any chance the suggestion above works for you?

andrueastman avatar Apr 03 '24 11:04 andrueastman

It did work! Thank you very much. I would have thought that Microsoft documentation would have had this as an example.

dernstOfExtron avatar Apr 03 '24 14:04 dernstOfExtron

Thanks for confirming. We'll keep this open to track that we need to add documentation for this scenario.

andrueastman avatar Apr 03 '24 15:04 andrueastman

This was helpful, thanks @eilonmore! +1 for adding it to the documentation, I spent quite some time looking for this (and figuring out a similar implementation myself) before stumbling across this issue.

parekhs-deshaw avatar Jan 14 '25 12:01 parekhs-deshaw

Why not implementing this directly as a credential class inside azure.identity.aio?

n3rada avatar Jun 23 '25 20:06 n3rada