【问题标题】:Do I need to refresh the access token when using Microsoft Graph and How to do it?使用 Microsoft Graph 时是否需要刷新访问令牌以及如何操作?
【发布时间】:2017-04-23 10:24:11
【问题描述】:

我正在为我的 iOS 应用程序使用 Microsoft Graph SDK。

访问令牌过期是否需要手动刷新?

我所说的访问令牌是: NXOAuth2AccountStore.sharedStore().accounts[0].accessToken

我已经测试过即使accessToken过期我仍然可以查询。在我第一次登录时,过期时间是 3600 秒。于是,我等了2个小时,再次测试获取用户信息,事件,仍然可以获取。

我已转储“accessToken.hasExpired”和“accessToken.expiresAt”以确保访问令牌已过期

谢谢

* 更多详情 *

我按照这里的示例进行操作: https://github.com/microsoftgraph/ios-swift-connect-sample

我在 Microsoft Graph 上找不到任何有关刷新访问令牌的文档: https://graph.microsoft.io/en-us/code-samples-and-sdks

【问题讨论】:

    标签: token access-token microsoft-graph-api refresh-token


    【解决方案1】:

    是的,在您的应用程序中使用 Graph 时,您需要定期刷新令牌。更详细的文档可通过 Azure AD 的站点获得:https://docs.microsoft.com/en-us/azure/active-directory/active-directory-authentication-scenarios

    您正在使用的建议身份验证库包含刷新此令牌的方法:

    @implementation NXOAuth2AuthenticatorRefreshCallback

    如果我没有回答您的问题,您能否更具体地说明您要完成的工作?您可以使用过期的令牌还是无法刷新旧令牌?

    【讨论】:

    • 嗨 Caitlin Bales, 感谢您的回答,我没有使用 Microsoft Azure Active Directory。我使用 Microsoft Graph,我在以下位置注册了一个应用程序:apps.dev.microsoft.com/Landing 我只是想知道,因为我已经测试过,并且看到访问令牌已过期,但我仍然可以发送任务(例如,获取日历中的事件)。跨度>
    • 是的,Microsoft Graph 使用 Azure AD 对用户进行身份验证(聚合身份验证:graph.microsoft.io/en-us/docs/authorization/converged_auth)。如有必要,我将调查过期令牌问题并为相关团队创建服务错误。
    【解决方案2】:

    在您需要刷新访问令牌时使用此代码。 这将作为图形 sdk 中提供的预定义代码的补丁,您可以从方法中提取令牌:

    +(id)tokenWithResponseBody:(NSString *)theResponseBody tokenType:(NSString *)tokenType;
    
    [MSGraphClient setAuthenticationProvider:AppDel.authentication.authProvider];
    
    _graphClient = [MSGraphClient client];
    
    NSMutableURLRequest * sampleReq = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://graph.microsoft.com/v1.0/me"]];
    
    [_graphClient.authenticationProvider appendAuthenticationHeaders:sampleReq completion:^(NSMutableURLRequest *request, NSError *error){    
        if(error == nil)
        {
    
        }
        else
        {
            [self showToast:@"" message:@"Failure in refresh 0365 token"];
        }
    }];
    

    【讨论】:

      猜你喜欢
      • 2022-01-12
      • 2019-09-19
      • 2022-08-11
      • 1970-01-01
      • 2019-12-29
      • 2019-10-26
      • 1970-01-01
      • 2020-07-12
      • 1970-01-01
      相关资源
      最近更新 更多