Question

I am trying to implement a tumblr login in my android app by following this example : https://github.com/jansanz/TumblrOAuthDemo.

i can get the request token . but when i'm trying to retrieve the AccessToken , it gives me an Exception. Here is the code :

public class TumblrDemoActivity extends Activity {

private static final String TAG = "TumblrDemo";

private static final String PREFS = "prefs_tumblr";
private static final String PREF_TOKEN = "pref_token_tumblr";
private static final String PREF_TOKEN_SECRET = "pref_token_secret_tumblr";

private static final String REQUEST_TOKEN_URL = "http://www.tumblr.com/oauth/request_token";
private static final String ACCESS_TOKEN_URL = "http://www.tumblr.com/oauth/access_token";
private static final String AUTH_URL = "http://www.tumblr.com/oauth/authorize";

// Taken from Tumblr app registration
private static final String CONSUMER_KEY = "consumer key from tumblr app";
private static final String CONSUMER_SECRET = "consumer secret from tumblr app";

private static final String CALLBACK_URL = "tumblrdemo://www.android-ios-tutorials.com";//"tumblrdemo://tumblrdemo.com/ok";

CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); 
CommonsHttpOAuthProvider provider = new CommonsHttpOAuthProvider(
        REQUEST_TOKEN_URL,
        ACCESS_TOKEN_URL,
        AUTH_URL);

SharedPreferences prefs;

String token;
String tokenSecret;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    prefs = getSharedPreferences(PREFS, Context.MODE_PRIVATE);
    consumer.setMessageSigner(new HmacSha1MessageSigner());
    // It uses this signature by default
    // consumer.setMessageSigner(new HmacSha1MessageSigner());

    // To get the oauth token after the user has granted permissions
    Uri uri = this.getIntent().getData();
    if (uri != null) {

        Log.d(TAG, "accessToken       : "+consumer.getToken());
        Log.d(TAG, "accessTokenSecret : "+consumer.getTokenSecret());

        //retrieve token and tokenSecret saved in retrieveRequestToken() call
        this.token = prefs.getString(PREF_TOKEN, null);
        this.tokenSecret = prefs.getString(PREF_TOKEN_SECRET, null);
        Log.d(TAG, "this.token       : "+token);
        Log.d(TAG, "this.tokenSecret : "+tokenSecret);

        consumer.setTokenWithSecret(token, tokenSecret);

        Log.d(TAG, "consumer.token       : "+consumer.getToken());
        Log.d(TAG, "consumer.tokenSecret : "+consumer.getTokenSecret());

        String token = uri.getQueryParameter("oauth_token");
        String verifier = uri.getQueryParameter("oauth_verifier");

        Log.v(TAG, "Token:" +token);
        Log.v(TAG, "Verifier:" +verifier);

        try {
            provider.retrieveAccessToken(consumer, verifier);
            Log.d(TAG, "accessToken       retrieveAccessToken    : "+consumer.getToken());
            Log.d(TAG, "accessTokenSecret retrieveAccessToken    : "+consumer.getTokenSecret());

        } catch (OAuthMessageSignerException e) {
            e.printStackTrace();
        } catch (OAuthNotAuthorizedException e) {
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            e.printStackTrace();
        }
    } else {
        String authUrl;
        try {
            authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
            Log.v(TAG, "Auth url:" + authUrl);
            Log.d(TAG, "accessToken       : "+consumer.getToken());
            Log.d(TAG, "accessTokenSecret : "+consumer.getTokenSecret());

            //save tokens in preferences
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString(PREF_TOKEN, consumer.getToken());
            editor.putString(PREF_TOKEN_SECRET, consumer.getTokenSecret());
            editor.commit();

            startActivity(new Intent("android.intent.action.VIEW", Uri.parse(authUrl)));

        } catch (OAuthMessageSignerException e) {
            e.printStackTrace();
        } catch (OAuthNotAuthorizedException e) {
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            e.printStackTrace();
        }
    }
}

@Override
protected void onResume() {
    super.onResume();
    Log.v(TAG, "onResume");
}

}

the first call of provider.retrieveRequestToken() is OK. i can retrieve the token and the tokenSecret. but when i try to get accessToken by calling provider.retrieveAccessToken(consumer,oauth_verifier); i got an OAuthCommunicationException the stack trace of the OAuthCommunicationException is :

    04-02 11:34:14.735: W/System.err(19535): oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: Service provider responded in error: 400 (Bad Request)
04-02 11:34:14.735: W/System.err(19535):    at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:218)
04-02 11:34:14.735: W/System.err(19535):    at oauth.signpost.AbstractOAuthProvider.retrieveAccessToken(AbstractOAuthProvider.java:108)
04-02 11:34:14.735: W/System.err(19535):    at org.jfsd.tumblrdemo.TumblrDemoActivity.onCreate(TumblrDemoActivity.java:87)
04-02 11:34:14.735: W/System.err(19535):    at android.app.Activity.performCreate(Activity.java:5066)
04-02 11:34:14.745: W/System.err(19535):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
04-02 11:34:14.745: W/System.err(19535):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
04-02 11:34:14.745: W/System.err(19535):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
04-02 11:34:14.745: W/System.err(19535):    at android.app.ActivityThread.access$600(ActivityThread.java:151)
04-02 11:34:14.745: W/System.err(19535):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
04-02 11:34:14.745: W/System.err(19535):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 11:34:14.755: W/System.err(19535):    at android.os.Looper.loop(Looper.java:155)
04-02 11:34:14.755: W/System.err(19535):    at android.app.ActivityThread.main(ActivityThread.java:5536)
04-02 11:34:14.755: W/System.err(19535):    at java.lang.reflect.Method.invokeNative(Native Method)
04-02 11:34:14.755: W/System.err(19535):    at java.lang.reflect.Method.invoke(Method.java:511)
04-02 11:34:14.755: W/System.err(19535):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
04-02 11:34:14.755: W/System.err(19535):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
04-02 11:34:14.755: W/System.err(19535):    at dalvik.system.NativeStart.main(Native Method)
04-02 11:34:14.765: W/System.err(19535): Caused by: oauth.signpost.exception.OAuthCommunicationException: Service provider responded in error: 400 (Bad Request)
04-02 11:34:14.765: W/System.err(19535):    at oauth.signpost.AbstractOAuthProvider.handleUnexpectedResponse(AbstractOAuthProvider.java:245)
04-02 11:34:14.765: W/System.err(19535):    at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:193)
04-02 11:34:14.765: W/System.err(19535):    ... 16 more

any help is apreciated.

Was it helpful?

Solution

I have created a small library for Tumblr Login using oauth sign post and jumblr. you can find it here :

Tumblr Login API Using oauth sign post

I hope that this library will be helpfull for others who have the same issue.

OTHER TIPS

I know this is a fairly old thread. But for anyone who comes in later.. I built a library to automate the login process. Besides, the link is broken Houcine. Might want to update that.

Its available on Github.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top