gpt4 book ai didi

c# - 另外获取刷新 token 以使用 Microsoft.Identity.Client 访问 token

转载 作者:行者123 更新时间:2023-12-03 03:29:50 24 4
gpt4 key购买 nike

我使用多个属性,例如租户 ID、客户端 ID、客户端 key 、重定向 URI 以及为用户生成的授权代码。我需要获取访问和刷新 token ,但 API 不会返回刷新 token 等任何内容。除了访问 token 和及时过期之外,我还需要刷新 token 。我使用以下代码:

ConfidentialClientApplicationOptions options = new ConfidentialClientApplicationOptions();
options.ClientId = clientId;
options.TenantId = tenantId;
options.ClientSecret = clientSecret;
options.RedirectUri = redirectUri;

ConfidentialClientApplicationBuilder builder = ConfidentialClientApplicationBuilder.
CreateWithApplicationOptions(options);
IConfidentialClientApplication app = builder.Build();
AcquireTokenByAuthorizationCodeParameterBuilder acquireTokenBuilder =
app.AcquireTokenByAuthorizationCode(ServiceConstants.ALL_SCOPE_AUTHORIZATIONS.Split(' '), authorizationCode);
AuthenticationResult result = await acquireTokenBuilder.ExecuteAsync();
string accessToken = result.AccessToken;
// NO string refreshToken = result.RefreshToken

这很奇怪,因为在几个示例中,我看到 AuthenticationResult 中提供了 RefreshToken,但我的中却没有。你知道为什么吗 ?我怎样才能获得刷新 token ?

因为之后我需要刷新访问 token 何时过期,并且我只有访问 token 、租户 ID、客户端 ID、 secret (或证书)和重定向 URI。顺便说一句,访问 token 过期后如何重新生成它?

非常感谢并致以最诚挚的问候

艾德里安

最佳答案

您需要检查 /authorize/token 请求中作为 ServiceConstants.ALL_SCOPE_AUTHORIZATIONS 传递的内容。范围列表应包含 offline_access 范围,因为它告诉 Azure 您的应用程序需要刷新 token 才能扩展对资源的访问。

刷新 token 的生命周期比访问 token 更长,因此每当您的访问 token 过期时,您都可以再次调用 /token 端点,提供之前收到的刷新 token 并使用参数grant_type=refresh_token

关于c# - 另外获取刷新 token 以使用 Microsoft.Identity.Client 访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74922956/

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