GraphServiceClient 使用授权令牌/刷新令牌机制

问题描述 投票:0回答:1

在我当前的应用程序中,我根据发送给用户的身份验证代码创建了刷新令牌/访问令牌对。我不断刷新此令牌(使用所述刷新令牌)以保证其完整性。当直接使用

msal
时,我会获取此令牌并将其直接传递到我的 HTTP 请求的标头中。我现在正在迁移到使用
msgraph
python sdk,但我找不到使用
azure.identity
实现相同流程的方法,以便创建
GraphServiceClient
并提出我的请求。

我可以使用刷新和访问令牌,以及客户端 ID 和密钥。

microsoft-graph-api azure-ad-msal azure-ad-graph-api microsoft-graph-sdks
1个回答
0
投票

好吧,我最终做了一些相当幼稚的事情,但它有效。不过,YMMV。同样重要的考虑因素是,该应用程序适用于委派权限,所以对我来说没有

ClientSecretCredentials
。我最终创建了类似于:

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)

并像这样使用它:

    def get_client_by_token_credential(self, token, expires_on) -> GraphServiceClient:
        credentials = RawAccessTokenProvider(token, expires_on)
        return GraphServiceClient(credentials=credentials)

所有功劳都归于答案这里

© www.soinside.com 2019 - 2024. All rights reserved.