5

From last 5 days I am search for a best working pice of code for twitter in android using OAuth... I found a lot. But not a single 1 is running perfectly. Any how i get some code. Its working but I have a problem. I am wondring what I have to write in String callBack = "?" so that my android browser redirect me to my application back instead of staying there after authentication.. If I am not using a CallBack and use OAuth.OUT_OF_BAND then browser show a pin code and dose not redirect browser back to my application. Please Help me to let me know what I am doing wrong.

Here my code goes

package com.example.tweeter;

import oauth.signpost.OAuth;
import oauth.signpost.OAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

public class Tweeter extends Activity {

    private String CONSUMER_KEY = "CONSUMER_KEY";
    private String CONSUMER_SECRET = "CONSUMER_SECRET";
    private static final Uri CALLBACK_URI = Uri.parse("PicPuzzle://tkxel");
    private String CALLBACK_URL = "PicPuzzle://tkxel";
    OAuthProvider provider ;
    CommonsHttpOAuthConsumer consumer ;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

        HttpClient client = new DefaultHttpClient();

        String authUrl = "http://www.yahoo.com";
        try {

            //This line work perfect but it not redirect me to my application
            authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);

            //I want to send a calll back but It give exception
            ///***  authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URI.toString());

        } catch (OAuthMessageSignerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthNotAuthorizedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
    }

    @Override
    protected void onResume() {
        // this must be places in activity#onResume()  
        Uri uri = this.getIntent().getData();  
        if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {  
            String verifier = uri.getQueryParameter("oauth_verifier");  
            // this will populate token and token_secret in consumer  
            try {
                provider.retrieveAccessToken(consumer, verifier);
            } catch (OAuthMessageSignerException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (OAuthNotAuthorizedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (OAuthExpectationFailedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (OAuthCommunicationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
        }  
        super.onResume();
    }
}

And my AndroidManifest.xml look like this

<uses-permission
    android:name="android.permission.INTERNET"/>
<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
>
    <activity
        android:name=".Tweeter"
        android:label="@string/app_name"
    >
        <intent-filter>
            <action
                android:name="android.intent.action.MAIN"/>
            <category
                android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <intent-filter>
        <action
            android:name="android.intent.action.VIEW"
        ></action>
        <category
            android:name="android.intent.category.DEFAULT"
        ></category>
        <category
            android:name="android.intent.category.BROWSABLE"
        ></category>
        <data
            android:scheme="PicPuzzle"
            android:host="tkxel"
        ></data>
    </intent-filter>
</application>
<uses-sdk
    android:minSdkVersion="7"/>`enter code here`
Justin
  • 800
  • 2
  • 11
  • 26
Arslan Anwar
  • 18,746
  • 19
  • 76
  • 105

1 Answers1

7

The callback URL should be based upon that which you configure for your activity in the manifest. Looks like you're using scheme="PicPuzzle", host="tkxel". So your callback URL is PicPuzzle://tkxel

I think the <data> tag should be on the particular Activity you want to receive the callback though (looks like you have it on the whole application at the moment).

Nightfirecat
  • 11,432
  • 6
  • 35
  • 51
ShibbyUK
  • 1,501
  • 9
  • 12
  • I try this to but still no solution...! It always show error 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. 01-10 16:53:20.614: WARN/System.err(5447): at oauth.signpost.AbstractOAuthProvider.handleUnexpectedResponse(AbstractOAuthProvider.java:239) 01-10 16:53:20.614: WARN/System.err(5447): at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:189) – Arslan Anwar Jan 10 '11 at 12:04
  • I've taken a closer look at your code. I can see a couple of potential issues: - Message signer: Set with "consumer.setMessageSigner(new HmacSha1MessageSigner());" - retrieving the access token. Use "OAuth.OAUTH_VERIFIER" for the verifier (although this is probably the same thing. – ShibbyUK Jan 10 '11 at 12:22
  • Thanks for consideration.... But where to put this code? Message signer: Set with "consumer.setMessageSigner(new HmacSha1MessageSigner());" – Arslan Anwar Jan 10 '11 at 12:59
  • Immediately after constructing your consumer. So just after: "consumer = new CommonsHttpOAuthConsumer( CONSUMER_KEY, CONSUMER_SECRET);" – ShibbyUK Jan 10 '11 at 13:36
  • One other thing to ensure: On your twitter application settings, ensure that "Application Type" is set to "Browser". You can use any callback URL here, as it is overridden by code. – ShibbyUK Jan 10 '11 at 13:59