Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 26th, 2012  |  syntax: None  |  size: 3.71 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.  * A very simple example to demonstrate how Twitter4J can be used to make the users
  3.  * sign-in with Twitter.
  4.  * Whenever it is started, control goes to twitter.com for authentication.
  5.  * if app is already authenticated, control comes back in your application.
  6.  * And finally it displays the first tweet of the timeline.
  7.  *
  8.  *
  9.  */
  10.  
  11.  
  12.  
  13. package com.T4J_OAuth.activities;
  14.  
  15. import java.util.List;
  16.  
  17. import twitter4j.Status;
  18. import twitter4j.Twitter;
  19. import twitter4j.TwitterException;
  20. import twitter4j.TwitterFactory;
  21. import twitter4j.http.AccessToken;
  22. import twitter4j.http.RequestToken;
  23. import android.app.Activity;
  24. import android.content.Intent;
  25. import android.net.Uri;
  26. import android.os.Bundle;
  27. import android.util.Log;
  28. import android.widget.Toast;
  29.  
  30. public class Main extends Activity {
  31.         /** Called when the activity is first created. */
  32.  
  33.         Twitter twitter;
  34.         RequestToken requestToken;
  35. //Please put the values of consumerKy and consumerSecret of your app
  36.         public final static String consumerKey = "YOUR_CONSUMER_KEY"; // "your key here";
  37.         public final static String consumerSecret = "YOUR_CONSUMER_SECRET"; // "your secret key here";
  38.         private final String CALLBACKURL = "T4JOAuth://main";  //Callback URL that tells the WebView to load this activity when it finishes with twitter.com. (see manifest)
  39.  
  40.  
  41.         /*
  42.          * Calls the OAuth login method as soon as its started
  43.          */
  44.         @Override
  45.         public void onCreate(Bundle savedInstanceState) {
  46.                 super.onCreate(savedInstanceState);
  47.                 setContentView(R.layout.main);
  48.                 OAuthLogin();
  49.         }
  50.  
  51.         /*
  52.          * - Creates object of Twitter and sets consumerKey and consumerSecret
  53.          * - Prepares the URL accordingly and opens the WebView for the user to provide sign-in details
  54.          * - When user finishes signing-in, WebView opens your activity back
  55.          */
  56.         void OAuthLogin() {
  57.                 try {
  58.                         twitter = new TwitterFactory().getInstance();
  59.                         twitter.setOAuthConsumer(consumerKey, consumerSecret);
  60.                         requestToken = twitter.getOAuthRequestToken(CALLBACKURL);
  61.                         String authUrl = requestToken.getAuthenticationURL();
  62.                         this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
  63.                                         .parse(authUrl)));
  64.                 } catch (TwitterException ex) {
  65.                         Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
  66.                         Log.e("in Main.OAuthLogin", ex.getMessage());
  67.                 }
  68.         }
  69.  
  70.        
  71.         /*
  72.          * - Called when WebView calls your activity back.(This happens when the user has finished signing in)
  73.          * - Extracts the verifier from the URI received
  74.          * - Extracts the token and secret from the URL
  75.          */
  76.         @Override
  77.         protected void onNewIntent(Intent intent) {
  78.                 super.onNewIntent(intent);
  79.                 Uri uri = intent.getData();
  80.                 try {
  81.                         String verifier = uri.getQueryParameter("oauth_verifier");
  82.                         AccessToken accessToken = twitter.getOAuthAccessToken(requestToken,
  83.                                         verifier);
  84.                         String token = accessToken.getToken(), secret = accessToken
  85.                                         .getTokenSecret();
  86.                         displayTimeLine(token, secret); //after everything, display the first tweet
  87.  
  88.                 } catch (TwitterException ex) {
  89.                         Log.e("Main.onNewIntent", "" + ex.getMessage());
  90.                 }
  91.  
  92.         }
  93.        
  94.         /*
  95.          * Displays the timeline's first tweet in a Toast
  96.          */
  97.  
  98.         @SuppressWarnings("deprecation")
  99.         void displayTimeLine(String token, String secret) {
  100.                 if (null != token && null != secret) {
  101.                         List<Status> statuses = null;
  102.                         try {
  103.                                 twitter.setOAuthAccessToken(token, secret);
  104.                                 statuses = twitter.getFriendsTimeline();
  105.                                 Toast.makeText(this, statuses.get(0).getText(), Toast.LENGTH_LONG)
  106.                                         .show();
  107.                         } catch (Exception ex) {
  108.                                 Toast.makeText(this, "Error:" + ex.getMessage(),
  109.                                                 Toast.LENGTH_LONG).show();
  110.                                 Log.d("Main.displayTimeline",""+ex.getMessage());
  111.                         }
  112.                        
  113.                 } else {
  114.                         Toast.makeText(this, "Not Verified", Toast.LENGTH_LONG).show();
  115.                 }
  116.         }
  117. }