gpt4 book ai didi

c# - 如何刷新 Microsoft Graph 的 token

转载 作者:太空狗 更新时间:2023-10-29 23:28:35 25 4
gpt4 key购买 nike

我正在使用以下方式连接到 Microsoft Graph:

public GraphServiceClient GetAuthenticatedClient(string token)
{
GraphServiceClient graphClient = new GraphServiceClient(
new DelegateAuthenticationProvider(
async (requestMessage) =>
{
// Append the access token to the request.
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
}));
return graphClient;
}

我在服务器上运行这段代码。我正在使用的 token 是由外部应用程序发送给我的。

第一个小时一切正常,然后 token 过期。

我的问题是:我如何才能获得新 token ,因为我也可以访问刷新 token ?

最佳答案

启用刷新 token 需要两个部分:

  1. 您需要请求范围 offline_access。这告诉端点提供 refresh_token 以及 access_token 和关联的元数据。

  2. 您需要通过重复相同的 POST/common/oauth2/v2.0/token 主体略有不同 - grant_type 设置为 refresh_token 而不是 code,您提供一个 refresh_token 属性和值:

    https://login.microsoftonline.com/common/oauth2/v2.0/token
    Content-Type: application/x-www-form-urlencoded

    grant_type=refresh_token&
    refresh_token=[REFRESH TOKEN]&
    client_id=[APPLICATION ID]&
    client_secret=[PASSWORD]&
    scope=[SCOPE]&
    redirect_uri=[REDIRECT URI]

不久前我写了一个节目primer on the v2 Endpoint您可能也会觉得有帮助。

关于c# - 如何刷新 Microsoft Graph 的 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51153055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com