13

I am trying the following sample app for twitter oauth.

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 ?

j0k
  • 22,600
  • 28
  • 79
  • 90
d-man
  • 57,473
  • 85
  • 212
  • 296
  • plz tell me what to put in CALLBACK_URL ??? – Shruti Sep 11 '12 at 09:58
  • your android activity URL that will get called after twitter returned the response – d-man Sep 11 '12 at 18:46
  • your android activity URL that will get called once twitter returned the response – d-man Sep 11 '12 at 18:46
  • so if i have to load a activity ..what shud i put in the callback url..presently i hav added someting.com in callback url..so that webpage is loaded..please guide me .. – Shruti Sep 12 '12 at 04:29
  • possible duplicate of [android twitter outh tutorial callback problem](http://stackoverflow.com/questions/3350895/android-twitter-outh-tutorial-callback-problem) – Bo Persson Nov 22 '12 at 11:56
  • May be you found solution here.... http://stackoverflow.com/a/20115215/2106820 – Arfan Mirza Nov 21 '13 at 07:52

6 Answers6

14

I have spent several hours on this. Seems that you have to set ANY value to the callback url in the Settings tab in your Twitter application developer panel. Keeping the default empty value disables dynamic callback urls.

All the tutorials and all the information I have found online is just void. Twitter long removed the "Client / Website" radio button.

Moreover, OAuth checks for clock skews.

gilm
  • 7,690
  • 3
  • 41
  • 41
8

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.

DonSteep
  • 1,075
  • 10
  • 10
  • 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. – DonSteep Jul 26 '10 at 09:44
  • 4
    +1, You are right about to set date and time in a phone which is without sim-card. – Paresh Mayani Aug 03 '11 at 08:38
4

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

2)

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) is your twitter app configured as Browser? try with this keys:

Consumer key

sdOjEI2cOxzTLHMCCMmuQ

Consumer secret

biI3oxIBX2QMzUIVaW1wVAXygbynuS80pqSliSDTc

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 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,. – d-man Jul 27 '10 at 04:24
  • 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 Jul 28 '10 at 15:36
  • Hello. I written a code in Titanium and it working perfectly when I use the Consumer key & Consumer secret key mentioned above. But when I create my own Application on Twitter it is not working. Can you please tell me what special changes you made in twitter application plz ? – AvtarSingh Suchariya Apr 12 '13 at 15:20
  • Yes setting correct date/time solved my issue, but why its like so? why twitter api needs correct date/time now? – Zoombie Sep 03 '13 at 13:20
  • 1
    great after setting date time app will working fine in devices – Ali Ahmad Nov 14 '13 at 08:55
2

using https in place of http in provider

El-tohamy
  • 21
  • 1
1

finally done, check out the following post

android twitter outh tutorial callback problem

Community
  • 1
  • 1
d-man
  • 57,473
  • 85
  • 212
  • 296
1

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
  • 2,064
  • 5
  • 24
  • 34