views:

370

answers:

4

I am trying the following sample app for twitter oath.

http://www.androidsdkforum.com/android-sdk-development/3-oauth-twitter.html

private void askOAuth() {
        try {
            consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
            provider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token",
                                                "http://twitter.com/oauth/access_token",
                                                "http://twitter.com/oauth/authorize");
            String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
            Toast.makeText(this, "Please authorize this app!", Toast.LENGTH_LONG).show();
            this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
        } catch (Exception e) {
            Log.e(APP, e.getMessage());
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

When i run the following code it gives exception as following

"oauth.signpost.exception.OAuthNotAuthorizedException: Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match."

on this line String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);

I provided the correct 'key' and 'secret' does twitter giving me wrong key and secret ?

A: 

I just had the same problem. It only appeared on my dev phone, but on the emulator and another phone the code worked fine. After trying out several solutions to related questions with no luck, eventually it turned out that I had not set the time and date on the dev phone, which doesn't have a sim-card in it. This caused SSL certificates to be invalid and OAuth request to fail, as well as anything else that used HTTPS. After setting the time the problems went away.

totramon
There seemed to be many other possible causes for the same issue, too, but this one I couldn't find suggested before, probably because it's not exactly a problem with Twitter or OAuth themselves.
totramon
A: 

1) Set date and time to the right values, this will help to fix this issue.

no this isn´t a joke! =)

private OAuthConsumer consumer;
private OAuthProvider provider;
...
...
...
provider = new CommonsHttpOAuthProvider (
                TWITTER_REQUEST_TOKEN_URL, 
                TWITTER_ACCESS_TOKEN_URL,
                TWITTER_AUTHORIZE_URL);

private void askOAuth() {
        try {
            consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
            provider = new CommonsHttpOAuthProvider("http://twitter.com/oauth/request_token",
                                                "http://twitter.com/oauth/access_token",
                                                "http://twitter.com/oauth/authorize");

            provider.setOAuth10a(true);

            String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
            Toast.makeText(this, "Please authorize this app!", Toast.LENGTH_LONG).show();
            this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
        } catch (Exception e) {
            Log.e(APP, e.getMessage());
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

3) Ok is your twitter app configured as Browser? try with this keys

Consumer key

sdOjEI2cOxzTLHMCCMmuQ

Consumer secret

biI3oxIBX2QMzUIVaW1wVAXygbynuS80pqSliSDTc

Jorgesys
i am trying above code in emulator and tested on device as well didn't work in both conditions.I will try again by setting emulator date and time and try out then,.
Faisal khan
@Faisal i have edited your code in my answer.
Jorgesys
@jorgesys i tried this but still not working getting same error.
Faisal khan
I have tested the example http://code.google.com/p/agirardello/ and I have no problems, are you sure your twitter app is configured as a Browser App???try with these keys Consumer key:sdOjEI2cOxzTLHMCCMmuQ Consumer secret:biI3oxIBX2QMzUIVaW1wVAXygbynuS80pqSliSDTc
Jorgesys
A: 

finally done, check out the following post

http://stackoverflow.com/questions/3350895/android-twitter-outh-tutorial-callback-problem

Faisal khan
ok I have asked several times "is ur twitter app configured as Browser???"
yes configured as browser.
Faisal khan
A: 

yes totramon is right...If you are facing problem only while authentication problem , you may have to set device time. I was facing the same problem and solved with this solution only. Also if you are using old twitter api , you need to change it to stable version api(2.1.4). You can find from the following link :

http://twitter4j.org/en/index.html

Enjoy..

Nirav Shah