How to refresh a token for Microsoft Graph(如何刷新 Microsoft Graph 的令牌)
问题描述
我正在使用以下方式连接到 Microsoft Graph:
I'm connecting to the Microsoft Graph using:
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;
}
我正在服务器上运行此代码.我正在使用的令牌是由外部应用发送给我的.
I'm running this code on the server. The token I'm using is being sent to me by an external App.
在第一个小时内一切正常,然后令牌过期.
Everything works great during the first hour, then the token expires.
我的问题是:我如何获得新令牌,因为我也可以访问刷新令牌?
My question is : How can I get a new token, since I also have access to the refresh token?
推荐答案
启用 Refresh Tokens 需要两个部分:
There are two pieces required to enable Refresh Tokens:
您需要请求范围
offline_access.这告诉端点提供refresh_token以及access_token和关联的元数据.
You need to request the scope
offline_access. This tells the endpoint to provide arefresh_tokenalongside theaccess_tokenand associated metadata.
您需要通过对 /common/oauth2/v2.0/token 的主体稍有不同 - grant_type 设置为 refresh_token 而不是 code,你提供一个 refresh_token 属性和值:
You need to request a new access_token (and refresh_token as they come together) by repeating the same POST to /common/oauth2/v2.0/token with a slightly different body - grant_type is set to refresh_token and instead of a code, you supply a refresh_token property and value:
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 你也可能会有所帮助.
A while back I wrote up a show primer on the v2 Endpoint that you might find helpful as well.
这篇关于如何刷新 Microsoft Graph 的令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何刷新 Microsoft Graph 的令牌
基础教程推荐
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
