Android Tumblr oAuth Confusion

I am trying to implement the Tumblr API in an android application. I really get hung up on user authorization so that they can do things like a message and view their dashboard. I really don't understand oAuth, and the Tumblr API skips right above it. I have no idea if I should ask the user for my credentials or what to do with the ones I have or something like that. I added the Signpost library to my project, but since then I have been scratching my head. Is anyone familiar with oAuth on Android who would like to fill me up? Thank!

+5
source share
2 answers

Yes, the documentation is not so good. You should first read about OAuth. Twitter has a good review.

First of all, you need a consumer key and a secret (you can get them by registering your application in tumblr). After that, you should use the URLs that Tumblr provides to get authorization from the user. Usually you will create a request URL from which you can send the user to a browser where he will log in and authorize your application. This will trigger a callback for your application and you can get the oAuth token. Save this in your application (SharedPreferences) so that you do not need to request authentication again from the user. With this token, you can interact with the Tumblr API, which requires authentication.

, - , . .

, signpost Tumblr. . :

. , :

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 : "tumblrapp://tumblrapp.com/ok". URL- Tumblr.

, , . , :

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

:

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

-. . , , .

+13

, , . Android GitHub, . , Jumblr.

1:

Bhargav Rao tttony Tumblr. , , , , ...

Tumblr - . , OAuth,

1) httpOAuthprovider signpost, .

            //Generate a new oAuthConsumer object
            commonsHttpOAuthConsumer
                    = new CommonsHttpOAuthConsumer(
                    "Consumer Key",
                    "Consumer Secret Key");
            //Generate a new oAuthProvider object
            commonsHttpOAuthProvider
                    = new CommonsHttpOAuthProvider(
                    "https://www.tumblr.com/oauth/request_token",
                    "https://www.tumblr.com/oauth/access_token",
                    "https://www.tumblr.com/oauth/authorize");
            //Retrieve the URL to which the user must be sent in order to authorize the consumer
            return commonsHttpOAuthProvider.retrieveRequestToken(
                    commonsHttpOAuthConsumer,
                    "Callback URL as registered with Tumblr"
            );

2) - URL-, Tumblr . -, . , WebView. WebView shouldOverrideUrlLoading. URL- . Post Authorization, tumblr a OAuthVerifier, .

public boolean shouldOverrideUrlLoading(WebView view, String strUrl) {
                //Log Current loading URL
                Log.i(TAG, strUrl);
                //Check if the Currently loading URL is that of the call back URL mentioned on top
                if (strUrl.toLowerCase().contains("Callback URL".toLowerCase())) {
                    //Parse string URL to conver to URI
                    Uri uri = Uri.parse(strUrl);
                    //instantiate String variables to store OAuth & Verifier tokens
                    String strOAuthToken = "";
                    String strOAuthVerifier = "";
                    //Iterate through Parameters retrieved on the URL
                    for (String strQuery : uri.getQueryParameterNames())
                        switch (strQuery) {
                            case "oauth_token":
                                //Save OAuth Token
                                //Note : This is not the login token we require to set on JumblrToken
                                strOAuthToken = uri.getQueryParameter(strQuery);
                                break;

                            case "oauth_verifier":
                                //Save OAuthVerifier
                                strOAuthVerifier = uri.getQueryParameter(strQuery);
                                break;
                        }
                }

3) OAuthVerifier .

try {
        //Queries the service provider for access tokens. The method does not return anything.
        //It stores the OAuthToken & OAuthToken secret in the commonsHttpOAuthConsumer object.
        commonsHttpOAuthProvider.retrieveAccessToken(commonsHttpOAuthConsumer, strOAuthVerifier);
        //Check if tokens were received. If Yes, save them to SharedPreferences for later use.
        if(!TextUtils.isEmpty(commonsHttpOAuthConsumer.getToken())) {
            Log.i(TAG, "OAuthToken : " + commonsHttpOAuthConsumer.getToken());
        }

        if(!TextUtils.isEmpty(commonsHttpOAuthConsumer.getTokenSecret())) {    
            Log.i(TAG, "OAuthSecretToken : " + commonsHttpOAuthConsumer.getTokenSecret());
        }
    } catch (OAuthCommunicationException e) {
        e.printStackTrace();
        return null;
    } catch (OAuthExpectationFailedException e) {
        e.printStackTrace();
        return null;
    } catch (OAuthNotAuthorizedException e) {
        e.printStackTrace();
        return null;
    } catch (OAuthMessageSignerException e) {
        e.printStackTrace();
        return null;
    }

4) Token TokenSecret Jumblr TumblrAPI.

+3

All Articles