如何为Microsoft Graph刷新令牌 [英] How to refresh a token for 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?
推荐答案
启用刷新令牌需要两个步骤:
There are two pieces required to enable Refresh Tokens:
-
您需要请求范围
offline_access
.这告诉端点在access_token
和关联的元数据旁边提供refresh_token
.
You need to request the scope
offline_access
. This tells the endpoint to provide arefresh_token
alongside theaccess_token
and associated metadata.
您需要通过重复相同的POST
至/common/oauth2/v2.0/token
并使用略有不同的正文来请求新的access_token
(以及refresh_token
一起出现)-grant_type
设置为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]
前一段时间,我在v2端点上写了一个节目入门可能也会有所帮助.
A while back I wrote up a show primer on the v2 Endpoint that you might find helpful as well.
这篇关于如何为Microsoft Graph刷新令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!