3

I have tried many different way and search all over the internet to find a tutorial to use JTwitter with OAuth. Here are the following step I have accomplish

Download both Jtwitter and Signpost Add them as Jars in the Java Builder

Created a simple button that run

public class ShareGenerator extends Activity {

    private static final String JTWITTER_OAUTH_KEY = "this_is_populated";
    private static final String JTWITTER_OAUTH_SECRET = "this_is_populated";

    Button menupopButton;

     @Override
        public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  setContentView(R.layout.share);
  this.setContentView(R.layout.share);
  this.txShare = (TextView)this.findViewById(R.id.lblshare);
  this.menupopButton = (Button)this.findViewById(R.id.menupop);


  menupopButton.setOnClickListener(new View.OnClickListener()
  { 
      public void onClick(View v) 

      {
          TwitterSend();

      } 
 }); 

     }

and I have my class

public void TwitterSend () {

                 OAuthSignpostClient client = new OAuthSignpostClient(JTWITTER_OAUTH_KEY, JTWITTER_OAUTH_SECRET, "oob");
                    Twitter jtwit = new Twitter("bob", client);
                    // open the authorisation page in the user's browser
                    client.authorizeDesktop();
                    // get the pin
                    String v = client.askUser("Please enter the verification PIN from Twitter");
                    client.setAuthorizationCode(v);
                    // Optional: store the authorisation token details
                    Object accessToken = client.getAccessToken();
                    // use the API!
                    jtwit.setStatus("Messing about in Java");

             }

However I can't even get the OAuth screen to pop up. It crashes when it get there. Can anyone help me at least see the OAuth screen? I do have the import set correctly.

JuniorFlip
  • 417
  • 2
  • 7
  • 17
  • Check out what Logcat tells about the crash, and make sure you have permission INTERNET in your manifest. – Pentium10 Jul 08 '10 at 08:47
  • The Logcat says java.lang.NoClassDefFoundErrors: java.awt.Desktop it is failing at the client.authorizeDesktop(); – JuniorFlip Jul 08 '10 at 16:32
  • I cannot convert URI to Uri in order to pass url in setData() method. Any suggestions to do so? –  Feb 03 '11 at 04:17

3 Answers3

3

The problem is in this line, which uses java.awt.Desktop:

// open the authorisation page in the user's browser
client.authorizeDesktop();

That will work on a desktop PC, but not on Android.

Instead, take the url from client.authorizeUrl(); and send the user there. E.g. with something like this:

URI url = client.authorizeUrl();
Intent myIntent = new Intent(Intent.VIEW_ACTION);
myIntent.setData(url);
startActivity(myIntent);

But I'm not an Android coder! You can almost certainly do better by using a callback instead of oob. Hopefully someone else can supply the code for that...

Daniel Winterstein
  • 2,418
  • 1
  • 29
  • 41
1

Daniel provides a pretty good starting point. This is how I implemented this in my Android app:

    OAuthSignpostClient authClient = new OAuthSignpostClient('apiKey','apiSecret','callbackUrl');

                java.net.URI jUrl = authClient.authorizeUrl();
                Uri.Builder uriBuilder = new Uri.Builder();
                uriBuilder.encodedPath(jUrl.toString());

                Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(jUrl.toString()));
                startActivity(myIntent);

of course, I used 'apiKey' and so on for brevity. You'll need to use your own key, secret and callback url in the OAuthSignpostClient constructor.

Note: you need to convert the Java.net.URI provided by JTwitter to an Android.net.Uri to use it to start a new intent.

After this you will need to use an intent-filter to catch the callback url and do something with the user token and secret you will get from the Twitter API.

Bob Vork
  • 2,927
  • 28
  • 32
  • I'm a bit confused about why you have the two lines with uriBuilder that you do not use. Does encodedPath has a hidden consequence or am I missing something? – Mikle Jan 17 '12 at 03:11
-2

see this post [Check rev history if you care]

Raju Jadhav
  • 57
  • 1
  • 6
  • 5
    You're going to find every related question and provide the same answer, aren't you? –  Nov 17 '10 at 15:24