2

I've a small app running on WP8.1 RT, which uses Live SDK to connect with Onedrive. Basically the code goes like this:

 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);

This works just fine, as I've wl.offline_access I get refresh tokens. As I've read tokens should be valid for a year.

But what to do when I get an exception The access token that was provided has expired.?
One of my users now has such a situation and I must say that I miss information what to do in such case, when using Live SDK api - as I've tried the authClient.CanLogout is always false and I cannot Logout() to sign in again. Does anybody knows something about this situation? Do I need to use REST api for this?

Romasz
  • 29,662
  • 13
  • 79
  • 154
  • The LiveConnectSession that can be found either in LiveLoginResult or LoginCompletedEventArgs's Session contains the RefreshToken. But how to use it I don't know, since LiveAuthClient does not take a refresh token anymore. – Barnstokkr Feb 04 '15 at 13:40
  • @Barnstokkr *LiveConnctSession* in `Microsoft.Live.dll, v5.6.0.0` for WP8.1 RT contains only two properties: *AccessToken* and *AuthenticationToken*. – Romasz Feb 04 '15 at 13:47

1 Answers1

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)
    };
}
Romasz
  • 29,662
  • 13
  • 79
  • 154
maniac
  • 159
  • 2
  • 10
  • But this requires using REST api. I wonder what to do in Live SDK API case. AFAIK it should automatically obtain handle refreshing, and in most cases does, but it turned out that in rare cases it ended in situation described in question. – Romasz Feb 13 '15 at 08:04
  • Just structure you code as if Token Expired Exception occurs then forward call to refresh token which is the code i have provided earlier. – maniac Feb 18 '15 at 10:22
  • Ok, then the question is from where I should get *UserRefreshToken* - the Live SDK API provides only authentication and access tokens. – Romasz Feb 18 '15 at 15:31
  • https://github.com/liveservices/LiveSDK-for-Windows/blob/master/src/Desktop/Samples/ApiExplorer/MainForm.cs – maniac Feb 19 '15 at 06:57
  • refer previous link hope it will help you – maniac Feb 19 '15 at 06:57
  • Or refer the whole Desktop or WebProject provided in that link – maniac Feb 19 '15 at 06:59