Authorizing on Windows 8

WInRT provides an extremely useful class called WebAuthenticationBroker which takes care of all the heavy lifting of displaying the site's web page for authorization and receiving the callback on the callback URL. This means that if you are writing a Windows 8 app you don't need to request xAuth to be enabled and just do the full OAuth flow.

To perform authorization on Windows 8 you will need to add a reference to:
  • DontPanic.TumblrSharp.dll
  • DontPanic.TumblrSharp.Client.dll (not necessary, but you will probably use it later)
  • DontPanic.TumblrSharp.WinRT.dll
The WinRT extensions in DontPanic.TumblrSharp.WinRT contain the extension method for the OAuthClient class that encapsulates the whole OAuth flow using the WebAuthenticationBroker. This means that you can receive the access token with a single method call like in the xAuth example:

namespace Sample.WinRT
{
    using DontPanic.TumblrSharp;
    using DontPanic.TumblrSharp.OAuth;
    using System.Threading.Tasks;

    public class TestOAuth
    {
        public async Task<Token> DoOAuthAsync()
        {
            string consumerKey = "<your consumer key here>";
            string consumerSecret = "<your consumer secret here>";

            //instantiate the OAuth client factory
            var factory = new OAuthClientFactory();

            //create the OAuthClient
            var oauthClient = factory.Create(consumerKey, consumerSecret);
            try
            {
                //perform the OAuth flow asynchronously; this will show Tumblr's
                //authorization page to the user and let the user authorize your
                //app to access his  account
                var token = await oauthClient.PerformOAuthAsync();
                if (token.IsValid)
                {
                    //we've got the token. Tumblr access tokens do not expire, 
                    //so it is a good idea to store it somewhere permanently so 
                    //that it can be used from now on
                    return token;
                }
                else
                {
                    //should not happen
                    return null;
                }
            }
            catch (OAuthException)
            {
                //an error occurred while performing xAuth
                return null;
            }
        }
    }
}





Last edited Aug 26, 2013 at 3:52 AM by turboronin, version 5