akohout
akohout

Reputation: 1811

Store LiveConnectSession in WP7

I want to use the Hotmail API on my Windows Phone 7 App. Therefore, I have to grant access to my mobile app. I do that using the Microsoft Live Connect API, which really works great for the start. I use the following code for authentication:

string[] requiredScope = { "wl.signin", "wl.calendars_update", "wl.offline_access" };
LiveAuthClient auth = new LiveAuthClient(Configuration.ClientID);
auth.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(authInitializeCompletedHandler);
auth.LoginAsync(requiredScope);

ClientID is the ID I got from MS for registering my app. My callback looks (in simple) like this:

private void authInitializeCompletedHandler(object sender, LoginCompletedEventArgs e)
{
    if (e.Status == LiveConnectSessionStatus.Connected)
    {
        this._session = e.Session; // Session hat AccessToken und RefreshToken
        this._isAuthorized = true;
        callDelegates();
    }
}

If that call succeeds (and it currently does, so that works), I store the retrieved Session. This session can be used in the LiveConnectClient constructor to enable the communication with the MS API's, in my app the Hotmail API.

LiveConnectClient client = new LiveConnectClient(_session);

My problem is that I want to save that session somehow, so that when the app is tombstoned and gets its focus back, I don't want the user to ask again for permission - he already gave me that. I have figured out by myself that the returned session contains a AccessToken, RefreshToken and Expires property (I know what that is, that's not the problem). How can I use that information in future calls? Microsoft's documentation about that ends here and I can't find any useful information.

2 workarounds I found:

But I don't really want to use that workarounds. I believe/hope that there is a better solution.

Upvotes: 4

Views: 1483

Answers (1)

VasiS
VasiS

Reputation: 116

Looks like the answer to this is as follows

  1. Make sure you applications include both wl.signin and wl.offline_access scopes.

  2. If you're not using the signin control provided in the SDK, you should always call LiveAuthClient.InitializeAsync when the app starts. Only call LiveAuthClient.LoginAsync if InitializeAsync does not return a valid session.

These steps will ensure you receiving a refresh token so no need to prompt for login or consent after the first time.

From: http://social.msdn.microsoft.com/Forums/en-US/messengerconnect/thread/6df1f31d-a22c-469c-b9f4-5c79e0882701

Upvotes: 6

Related Questions