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_token
alongside theaccess_token
and 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 的令牌
基础教程推荐
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30