Building a Twitter client for Windows Phone - Storing the auth data and making sure the user is remembered

DZone 's Guide to

Building a Twitter client for Windows Phone - Storing the auth data and making sure the user is remembered

· Mobile Zone ·
Free Resource

Previous parts:

The user finally logged in and the application got the necessary permissions. It is now time to make sure that the credentials are properly stored and are re-used on the next application launch. Another feature that needs to be implemented along the lines is the ability to log out and let the user change accounts, if that is necessary. So let's look at what we have after the final authentication request.

NOTE: The token and token secret pieces you see here won't help you to impersonate the app.

So there you have it - oauth_token, oauth_token_secret, user_id and screen_name. The first two parameters will be used to sign all general requests, and the user id and screen name are provided for informational purposes. Nonetheless, we need to store everything.

In my Utility folder, I created an Isolated Storage helper class - IsoStoreHelper that will be used to facilitate storage operations inside MangoTree. The first method is going to handle the storage of user credentials:

public static void StoreNewCredentials(string oAuthToken, string oAuthTokenSecret, string userid, string screenName)
    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
    settings["oauth_token"] = oAuthToken;
    settings["oauth_token_secret"] = oAuthTokenSecret;
    settings["userid"] = userid;
    settings["screen_name"] = screenName;

Going back to the OAuthClient class, to the part where I am handling the response from the server, I simply need to add one line, calling StoreNewCredentials:

IsoStoreHelper.StoreNewCredentials(App.Token, App.TokenSecret, App.UserID, App.UserName);

So now all you need to do is check whether the data is already stored. To do this, I created another method in my IsoStoreHelper class, called CheckIfAuthorized:

public static bool CheckIfAuthorized()
    var settings = IsolatedStorageSettings.ApplicationSettings;
    settings.TryGetValue("oauth_token", out App.Token);
    settings.TryGetValue("oauth_token_secret", out App.TokenSecret);
    settings.TryGetValue("userid", out App.UserID);
    return settings.TryGetValue("screen_name", out App.UserName);

You might be wondering - how come I am only making sure that one auth pair is present? Simply because when I decide to clear the user authentication data, I will remove all pairs - there will never be a situation when one pair is present without the associated ones.

The verification in my case is done inside App.xaml.cs. Ultimately, we have multiple choices of ways to make sure that the correct page is shown - for example, load the main working page and then navigate to the auth page. I will let the root frame navigate to the work page directly, if possible.

// Do not add any additional code to this method
private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
    // Set the root visual to allow the application to render
    if (RootVisual != RootFrame)
        RootVisual = RootFrame;

    if (IsoStoreHelper.CheckIfAuthorized())
        RootFrame.Navigate(new Uri("/WorkPage.xaml", UriKind.Relative));

    // Remove this handler since it is no longer needed
    RootFrame.Navigated -= CompleteInitializePhoneApplication;

I know, that commend above the event handler says that we should not add code to the block below. But since my call is not resource-intensive, it is all good. So what's the drawback of this way of navigation? The initial page - MainPage.xaml will still be registered as the main opening point for the root frame. Even with the proper credentials stored, the user will be able to click on Back and see the auth page. This behavior can be prevented in the work page, but we can leave it as it is for now.

WorkPage.xaml is a blank portrait Windows Phone page - feel free to add it manually to your project.

Congratulations, now we can finally work on the "cool" Twitter stuff - setting statuses, uploading images and analyzing your news feed.

Before I will tell you what to do next, make sure you read through these articles - we will use them later in the series:


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}