5

i have loads of examples for O_Auth but no one is working properly and i guess the only thing is callback URL in every example, I am getting 401 error like consumer key or signature didn't match. I already seen so many examples on 401 the only thing that goes into my favor is my callback url.

android app cannot connect to twitter. Getting 401 when requesting access token with signpost within android with-signpost-within-android Android Dev - Callback URL not working... (0_o)

I already seen all these examples and many more.

But still i am not getting my point, If at the time of application form i use http://www.meomyo.com at callback url, Then what should i use it in my coding like android:scheme="?" android:host="?" currrently i am using other examples callbacks

    //private static final Uri CALLBACK_URI = Uri.parse("bloa-app://twitt");
private static final Uri CALLBACK_URI = Uri.parse("twitterapp://connect");

i have consumer key as well secret key, but in case of callback url i am stuck on it. If anyone want i can provide my consumer as well secret key.

public class OAuth extends Activity {

private static final String APP =   "OAUTH";

private Twitter twitter;
private OAuthProvider provider;
private CommonsHttpOAuthConsumer consumer;

private String CONSUMER_KEY =       "Xh3o8Gh1JQnklkUnTvvA";
private String CONSUMER_SECRET =    "SCLy6yoUSth53goAsYYkoqR4ZuBoaInyJXsm5PQR11I";
private String CALLBACK_URL =       "merabharatmahan://piyush";

private TextView tweetTextView;
private Button buttonLogin;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tweetTextView = (TextView)findViewById(R.id.tweet);
    buttonLogin = (Button)findViewById(R.id.ButtonLogin);
    buttonLogin.setOnClickListener(new OnClickListener() {  
        public void onClick(View v) {
            askOAuth();
        }
    });
}

/**
 * Open the browser and asks the user to authorize the app.
 * Afterwards, we redirect the user back here!
 */
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();
    }
}


/**
 * As soon as the user successfully authorized the app, we are notified
 * here. Now we need to get the verifier from the callback URL, retrieve
 * token and token_secret and feed them to twitter4j (as well as
 * consumer key and secret).
 */
@Override
protected void onNewIntent(Intent intent) {

    super.onNewIntent(intent);

    Uri uri = intent.getData();
    if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {

        String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);

        try {
            // this will populate token and token_secret in consumer
            provider.retrieveAccessToken(consumer, verifier);

            // TODO: you might want to store token and token_secret in you app settings!!!!!!!!
            AccessToken a = new AccessToken(consumer.getToken(), consumer.getTokenSecret());

            // initialize Twitter4J
            twitter = new TwitterFactory().getInstance();
            twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
            twitter.setOAuthAccessToken(a);

            // create a tweet
            Date d = new Date(System.currentTimeMillis());
            String tweet = "#OAuth working! " + d.toLocaleString();

            // send the tweet
            twitter.updateStatus(tweet);

            // feedback for the user...
            tweetTextView.setText(tweet);
            Toast.makeText(this, tweet, Toast.LENGTH_LONG).show();
            buttonLogin.setVisibility(Button.GONE);

        } catch (Exception e) {
            Log.e(APP, e.getMessage());
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }

    }
}

}

manifest code is

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".OAuth"
              android:label="@string/app_name"
              android:launchMode="singleInstance">
        <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="merabharatmahan" android:host="piyush" />
        </intent-filter>
    </activity>

</application

now i am getting communication with the service provider is failed: Received Authentication challenge is null. It is the simplest example that i put here.

Community
  • 1
  • 1
PiyushMishra
  • 5,743
  • 6
  • 37
  • 57
  • as per above manifest, first of all change android:launchMode="singleTask", android:scheme and host looks ok to me. – Zoombie May 10 '11 at 04:41
  • let us know which authentication jar you are using in android? I am using twitter4j, signpost-core & signpost-commonshttp jar for oAuth implementation. – Zoombie May 10 '11 at 04:42
  • `try { consumer.setTokenWithSecret(tempToken, tempSecretKey); provider.retrieveAccessToken(consumer, verifier); HttpParameters params = provider.getResponseParameters(); String userName = params.getFirst("screen_name"); String returnToken = consumer.getToken(); String returnSecret = consumer.getTokenSecret();` – Zoombie May 10 '11 at 04:47

2 Answers2

8

twitterapp://connect is your call back url.

In Android manifest.xml define your activity like this:

<activity android:name=".auth.SocialNetworkActivity"
    android:configChanges="orientation"
        android:label="Connect SNS" android:launchMode="singleTask">
        <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="twitterapp" android:host="connect" />
        </intent-filter>
    </activity>

Now, it will open twitter web page, and on successful authentication, your activities onNewIntent() callback method will be called.

In above example, use your own activity name.

Apart from that, twitter integration in my application is done using twitter4j.

Zoombie
  • 3,590
  • 5
  • 33
  • 40
  • Hi Zoombie, i just put a sample one for reference still i am not getting any token, i am able to get it on java with the help of DefaultOAuth.* but android didn't support it and my all samples are saying you need signature key and in my java program i am using a SignatureMethod.HMAC_SHA1 and i am getting the token but in case of android... :( – PiyushMishra May 09 '11 at 14:26
  • +1. I'd +27 it if I could. Worth mentioning that when you register your app with Twitter, you have to say it's a browser app (and give an actual internet callback URL), even if it's a desktop app, or it won't work. – TarkaDaal Jun 18 '11 at 19:39
  • @Dan: yes absolutely correct, need to provide information whether appln on twitter is browser/client based app. If browser then callback url is necessary. – Zoombie Jun 21 '11 at 04:14
1

You must put whatever you think will be unique:

android:scheme="name-of-your-dog" android:host="something-else"

Oh, wait... maybe there's someone else owning a dog called like yours... so, use the package name of your app:

android:scheme="com.your.package" android:host="something-else"

And, if it's not working for you... I don't think it's related to the callback URL. How are you declaring your activity in the manifest? You added the android:launchMode="singleTask" parameter, right?

And, as the zombie guy pointed out, you must handle the callback in the onNewIntent method of your auth activity. Something like this:

final Uri uri = intent.getData();
if (uri != null && uri.getScheme().equals("name-of-your-dog")) {
    //etc...
}
Cristian
  • 198,401
  • 62
  • 356
  • 264
  • still not working, i am working on it from last two days continuously, one more thing to ask i see various samples those who are use callback url into their activity and they are defining the things into their manifest also like category and data but i am using the call back into a simple java class not an activity so is it the reason that manifest is not recognize it coz i am not defining anywhere in manifest category and data coz its java file. – PiyushMishra May 09 '11 at 13:48
  • Yes you have to define that in manifest first, and code should be in activity, my answer below just explains that only – Zoombie May 09 '11 at 13:55
  • @PiyushMishra: it's mandatory to do the android manifest stuff. Just see how @zoombie made it in his answer... it's that easy. – Cristian May 09 '11 at 14:03
  • Thanks a lot guys let me work on it and thanks for your precious time that you spend on me. :)) – PiyushMishra May 09 '11 at 14:10
  • Hi Cristain i just put a sample one for you, please see where is the prob or if you have any working example please send me the link i have already 3-4 example but in all i am getting 401 error. i got the token when i am using java with DefaultOAuth but giving SignatureMethod.HMAC_SHA1 but in case of android it wont work coz it didnt support and rest you suggest me what to do next? – PiyushMishra May 09 '11 at 14:29
  • 1
    What OAuth library are you using? Sign post? And what's exactly your error? Aren't your activity being called back after the twitter page is launched? Where are you launching the twitter from? – Cristian May 09 '11 at 16:17
  • @Cristain thanks a lot of helping me......i got it and your answer give me a lot of help......thanks once again. – PiyushMishra May 10 '11 at 16:20