如何刷新 Microsoft Graph 的令牌 [英] How to refresh a token for Microsoft Graph

查看:42
本文介绍了如何刷新 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:

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

  1. You need to request the scope offline_access. This tells the endpoint to provide a refresh_token alongside the access_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 的令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆