Android Open Source - ara-twitter Twitter O Auth






From Project

Back to project page ara-twitter.

License

The source code is released under:

Apache License

If you think the Android project ara-twitter listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.bakingcode.io.twitter;
/*  w w  w . j a va 2s . co m*/
import android.os.AsyncTask;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;

import static com.bakingcode.io.twitter.tools.TwitterLogging.LT;

/**
 * This class provides oauth abstraction. To use this:
 * 
 * - Create a new instance with a simple new TwitterOauth().
 * - Retrieve request token (retrieveRequestToken())
 * - When received message in OauthLister load the url received and authorize the app (user fill fields)
 * - call accessToken with the verifier received
 * - Wait for message access token received in the listener
 * - Ready to use the token and token secret.
 *
 */
public class TwitterOAuth {
  
  // ///////////////////////////////////////////////////////////////////////////
  // Constants
  // ///////////////////////////////////////////////////////////////////////////

    /**
     * Logging tag
     */
    private static final String TAG = "TwitterOAuth";

  /**
   * Twitter request URL
   */
  public static final String REQUEST_URL = "https://api.twitter.com/oauth/request_token";

  /**
   * Twitter access URL
   */
  public static final String ACCESS_URL = "https://api.twitter.com/oauth/access_token";

  /**
   * Twitter Authorize URL
   */
  public static final String AUTHORIZE_URL = "https://api.twitter.com/oauth/authorize";
  
  // ///////////////////////////////////////////////////////////////////////////
  // Oauth Vars
  // ///////////////////////////////////////////////////////////////////////////
  
  /**
   * OAuth consumer for provide signing on petitions
   */
  private OAuthConsumer consumer = null;
  
  /**
   * Oauth provider
   */
  private CommonsHttpOAuthProvider provider = new CommonsHttpOAuthProvider(REQUEST_URL, ACCESS_URL, AUTHORIZE_URL);
  
  /**
   * Default constructor. Provide a consumer key and consumer secret to retrieve token and token secret. 
   * 
   * @param consumerKey consumer key
   * @param consumerSecret consumer secret
   */
  public TwitterOAuth(String consumerKey, String consumerSecret) {
    
    consumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
    provider.setOAuth10a(true);

  }

  /**
   * Retrieves the request token
   * 
   * @param oauthUrlCallBack configured oauth callback
   * @param listener listener
   */
  public void retrieveRequestToken(String oauthUrlCallBack, OAuthListener listener) {
    
    new RetrieveRequestToken(listener).execute(oauthUrlCallBack);
    
  }
  
  /**
   * Retrieves the access token
   * 
   * @param verifier verifier
   * @param listener listener
   */
  public void retrieveAccessToken(String verifier, OAuthListener listener) {
    
    new RetrieveAccessToken(listener).execute(verifier);
    
  }

  /**
   * AsyncTask to retrieve access token
   */
  class RetrieveAccessToken extends AsyncTask<String, Void, Void> {

    /**
     * Listener to notify OauthMessages
     */
    private OAuthListener listener;
    
    public RetrieveAccessToken(OAuthListener listener) {
      this.listener = listener;
    }
    
    @Override
    protected Void doInBackground(String... params) {
      
      String verifier = params[0];
      try {
        
        provider.retrieveAccessToken(consumer, verifier);
        
      } catch (Throwable e) {
        LT(TAG, e);
      }
      
      
      return null;
    }
    
    protected void onPostExecute(Void result) {
      
      // Request twitter
      String token = consumer.getToken();
      String tokenSecret = consumer.getTokenSecret();
      
      if (listener != null) {
        listener.onAccessTokenReceived(token, tokenSecret);
      }
      
    }
    
  }
    
  /**
   * Asynctask to retrieve request token
   */
  class RetrieveRequestToken extends AsyncTask<String, Void, String> {
    
    /**
     * OAuthListener for notify oauth messages
     */
    private OAuthListener listener;
    
    public RetrieveRequestToken(OAuthListener listener) {
      this.listener = listener;
    }
    
    @Override
    protected String doInBackground(String... params) {
      
      String oauthCallBack = params[0];
      
      try {
        
        return provider.retrieveRequestToken(consumer, oauthCallBack);
        
      } catch (Throwable e) {
                LT(TAG, e);
      }
      
      return null;
    }
    
    protected void onPostExecute(final String authUrl) {
      
      if (listener != null) {
        listener.onRequestTokenReceived(authUrl);
      }
      
    }
    
  }

  /**
   * Oauth Message listener
   */
  public interface OAuthListener {

    /**
     * Called when the request token is received
     * @param requestToken requestToken
     */
    public void onRequestTokenReceived(String requestToken);
    
    /**
     * Called when the access token is received
     * @param token token 
     * @param tokenSecret tokenSecret
     */
    public void onAccessTokenReceived(String token, String tokenSecret);
    
  }
  
}




Java Source Code List

com.bakingcode.io.twitter.Parameters.java
com.bakingcode.io.twitter.TwitterHttpClient.java
com.bakingcode.io.twitter.TwitterOAuth.java
com.bakingcode.io.twitter.Twitter.java
com.bakingcode.io.twitter.Urls.java
com.bakingcode.io.twitter.exceptions.TwitterCommunicationException.java
com.bakingcode.io.twitter.exceptions.TwitterErrorRequestException.java
com.bakingcode.io.twitter.exceptions.TwitterException.java
com.bakingcode.io.twitter.model.ApiConfiguration.java
com.bakingcode.io.twitter.model.ApiMethod.java
com.bakingcode.io.twitter.model.ApiResourceFamily.java
com.bakingcode.io.twitter.model.Coordinates.java
com.bakingcode.io.twitter.model.Cursor.java
com.bakingcode.io.twitter.model.Friendship.java
com.bakingcode.io.twitter.model.IError.java
com.bakingcode.io.twitter.model.Id.java
com.bakingcode.io.twitter.model.IdentifierList.java
com.bakingcode.io.twitter.model.MediaEntity.java
com.bakingcode.io.twitter.model.Place.java
com.bakingcode.io.twitter.model.ResponseString.java
com.bakingcode.io.twitter.model.TrendPlace.java
com.bakingcode.io.twitter.model.Trend.java
com.bakingcode.io.twitter.model.Tweet.java
com.bakingcode.io.twitter.model.TwitterError.java
com.bakingcode.io.twitter.model.User.java
com.bakingcode.io.twitter.tools.HTMLEntity.java
com.bakingcode.io.twitter.tools.Tools.java
com.bakingcode.io.twitter.tools.TwitterLogging.java