Search code examples
androidoauthtumblrsignpost

Android Tumblr oAuth Confusion


I'm trying to implement the Tumblr API in an Android app. I'm really getting stuck with authorizing a user so that they can do things such as post and view their dashboard. I don't really understand oAuth, and the Tumblr API documentation sort of skips right over it. I have no idea if I'm supposed to prompt the user for their credentials, or what to do with those once I have them, or anything like that. I added the Signpost library to my project, but I've been scratching my head since then. Anybody familiar with oAuth on Android who would care to fill me in? Thanks!


Solution

  • Yes, the documentation is not that good. You should first read about OAuth. Twitter has a good overview.

    First of all you need a consumer key and secret (you can get those by registering your app in tumblr). After that, you should use the auth URLs that Tumblr provides to get the authorization from the user. Usually you will generate a request URL, from which you can take the user to the browser where he/she will login and authorize your app. This will trigger a callback to your app, and you will be able to get the oAuth token. Save this in your app (SharedPreferences) so that you won't need to ask the user again to authenticate. With this token you will be able to interact with Tumblr's API that requires authentication.

    Note that you could also implement a webview instead of making the user use the browser. Though, this requires a bit more of work.

    I have found that the latest signpost library does not work well with Tumblr. You will need a bit older version. Head here and download these files:

    Import both libraries to your project. To use them, basically you need to call the following code:

    CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY,
             CONSUMER_SECRET);
    CommonsHttpOAuthProvider provider = new CommonsHttpOAuthProvider(
             REQUEST_TOKEN_URL,
             ACCESS_TOKEN_URL,
             AUTH_URL);
    String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL); 
    

    CALLBACK_URL could be something like this: "tumblrapp://tumblrapp.com/ok". There is no need to set the callback URL on the Tumblr settings.

    Also, you will need to set an intent filter so your app gets called after authorization. Make sure that your Manifest looks like this:

                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
                <intent-filter>
                    <action android:name="android.intent.action.VIEW"/>
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE"/>
                    <data android:scheme="tumblrapp"/>
                </intent-filter>
    

    Now after authentication you can get the token like this:

    Uri uri = this.getIntent().getData();
    if (uri != null) {
       String token = uri.getQueryParameter("oauth_token");
    }
    

    I made a quick sample app. You can check it out here. You might want to move the request to a background thread as it will block the UI.