令牌已过期 - 接下来该怎么做?

时间:2015-02-04 06:58:28

标签: c# windows-runtime windows-phone windows-phone-8.1 onedrive

我在WP8.1 RT上运行的小应用程序,它使用Live SDK连接Onedrive。基本上代码是这样的:

 string[] scopes = new string[] { "wl.signin", "wl.skydrive", "wl.offline_access" };
 authClientOnedrive = new LiveAuthClient();
 LiveLoginResult loginResult = await authClientOnedrive.InitializeAsync(scopes);
 if (loginResult.Status != LiveConnectSessionStatus.Connected)
     await authClientOnedrive.LoginAsync(scopes);
 clientOnedrive = new LiveConnectClient(authClientOnedrive.Session);

这很好用,因为我已经 wl.offline_access 我得到刷新令牌。因为我读过的令牌应该有效一年。

但是当我得到例外The access token that was provided has expired.时该怎么做? 我的一个用户现在有这样的情况,我必须说我在这种情况下错过了什么信息,当使用Live SDK api时 - 因为我已经尝试了 authClient.CanLogout 总是 false 我无法 Logout()再次登录。有谁知道这种情况?我需要使用REST api吗?

1 个答案:

答案 0 :(得分:0)

public async Task<UserToken> RefreshAccessTokenAsync()
{
    var refreshAccessToken = RequestGenerator.RefreshAccessToken(_options.ClientId, _options.ClientSecret, _options.CallbackUrl, UserRefreshToken);
    var token = await ExecuteAuthorization<UserToken>(refreshAccessToken);

    _options.AccessToken = token.Access_Token;
    _options.RefreshToken = token.Refresh_Token;

    return token;
}

public IRequest RefreshAccessToken(string clientId, string clientSecret, string callbackUrl, string refreshToken)
{
    var nvc = new Dictionary<string, string>();
    nvc["client_id"] = clientId;
    nvc["redirect_uri"] = callbackUrl;
    nvc["client_secret"] = clientSecret;
    nvc["refresh_token"] = refreshToken;
    nvc["grant_type"] = "refresh_token";

    return new Request
    {
        BaseAddress = OAuthUrlBase,
        Resource = OAuthResource.Replace("{verb}", TokenVerb),
        Method = HttpMethod.Post,
        Content = new FormUrlEncodedContent(nvc)
    };
}