Android Open Source - TButler O A U T H






From Project

Back to project page TButler.

License

The source code is released under:

Tbutler includes software from Twitter4J to parse Twitter response from the Twitter API. You can see the license term at http://twitter4j.org/en/index.html#license Copyright (c) 2009-2010, David Parr...

If you think the Android project TButler 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.davidparry.twitter;
//w w  w  . j a  va2  s  .c o  m
import junit.framework.Assert;
import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
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 android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.davidparry.twitter.oath.TButlerOATH;

public class OAUTH extends Activity implements TButlerOATH{
    private static final String TAG = "OAUTH";
   
  
    private OAuthConsumer mConsumer = null;
    private OAuthProvider mProvider = null;
   
    SharedPreferences mSettings;
   
    public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
       
      mConsumer = new CommonsHttpOAuthConsumer(
          CONSUMER_KEY, 
          CONSUMER_SECRET);
   
      mProvider = new CommonsHttpOAuthProvider (
          TWITTER_REQUEST_TOKEN_URL, 
          TWITTER_ACCESS_TOKEN_URL,
          TWITTER_AUTHORIZE_URL);
      mSettings = this.getSharedPreferences("TWITTER_BUTLER", Context.MODE_PRIVATE);
   
      Intent i = this.getIntent();
      if (i.getData() == null) {
        try {
          String url = REDIRECT_URI.toString();
          //String url = CALLBACK_URI.toString();
          url  = Uri.encode(url);
          url = "";
          String authUrl = mProvider.retrieveRequestToken(mConsumer, url);
          saveRequestInformation(mSettings, mConsumer.getToken(), mConsumer.getTokenSecret());
          this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
          
        } catch (OAuthMessageSignerException e) {
          e.printStackTrace();
        } catch (OAuthNotAuthorizedException e) {
          e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
          e.printStackTrace();
        } catch (OAuthCommunicationException e) {
          e.printStackTrace();
        }
      }
    }
   
    @Override
    protected void onResume() {
      super.onResume();
   
      Uri uri = getIntent().getData();
      if (uri != null && CALLBACK_URI.getScheme().equals(uri.getScheme())) {
        String token = mSettings.getString(OAUTH.REQUEST_TOKEN, null);
        String secret = mSettings.getString(OAUTH.REQUEST_SECRET, null);
        Intent i = new Intent(this, ButlerTabActivity.class); // Currently, how we get back to main activity.
   
        try {
          if(!(token == null || secret == null)) {
            mConsumer.setTokenWithSecret(token, secret);
          }
          String otoken = uri.getQueryParameter(OAuth.OAUTH_TOKEN);
          String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
   
          // We send out and save the request token, but the secret is not the same as the verifier
          // Apparently, the verifier is decoded to get the secret, which is then compared - crafty
          // This is a sanity check which should never fail - hence the assertion
          Assert.assertEquals(otoken, mConsumer.getToken());
   
          // This is the moment of truth - we could throw here
          mProvider.retrieveAccessToken(mConsumer, verifier);
          // Now we can retrieve the goodies
          token = mConsumer.getToken();
          secret = mConsumer.getTokenSecret();
          OAUTH.saveAuthInformation(mSettings, token, secret);
          // Clear the request stuff, now that we have the real thing
          OAUTH.saveRequestInformation(mSettings, null, null);
          i.putExtra(USER_TOKEN, token);
          i.putExtra(USER_SECRET, secret);
        } catch (Exception e) {
        
        } finally {
          startActivity(i); // we either authenticated and have the extras or not, but we're going back
          finish();
        }
      }
    }
   
    public static void saveRequestInformation(SharedPreferences settings, String token, String secret) {
      // null means to clear the old values
      SharedPreferences.Editor editor = settings.edit();
      if(token == null) {
        editor.remove(OAUTH.REQUEST_TOKEN);
        Log.d(TAG, "Clearing Request Token");
      }
      else {
        editor.putString(OAUTH.REQUEST_TOKEN, token);
        Log.d(TAG, "Saving Request Token: " + token);
      }
      if (secret == null) {
        editor.remove(OAUTH.REQUEST_SECRET);
        Log.d(TAG, "Clearing Request Secret");
      }
      else {
        editor.putString(OAUTH.REQUEST_SECRET, secret);
        Log.d(TAG, "Saving Request Secret: " + secret);
      }
      editor.commit();
   
    }
   
    public static void saveAuthInformation(SharedPreferences settings, String token, String secret) {
      // null means to clear the old values
    SharedPreferences.Editor editor = settings.edit();
      if(token == null) {
        editor.remove(OAUTH.USER_TOKEN);
        Log.d(TAG, "Clearing OAuth Token");
      }
      else {
        editor.putString(OAUTH.USER_TOKEN, token);
        Log.d(TAG, "Saving OAuth Token: " + token);
      }
      if (secret == null) {
        editor.remove(OAUTH.USER_SECRET);
        Log.d(TAG, "Clearing OAuth Secret");
      }
      else {
        editor.putString(OAUTH.USER_SECRET, secret);
        Log.d(TAG, "Saving OAuth Secret: " + secret);
      }
      editor.commit();
   
    }
   
  }




Java Source Code List

com.davidparry.twitter.AlarmReceiver.java
com.davidparry.twitter.ButlerActivity.java
com.davidparry.twitter.ButlerException.java
com.davidparry.twitter.ButlerTabActivity.java
com.davidparry.twitter.FileListActivity.java
com.davidparry.twitter.InfoActivity.java
com.davidparry.twitter.ItemViewActivity.java
com.davidparry.twitter.OAUTH.java
com.davidparry.twitter.ProfileActivity.java
com.davidparry.twitter.ServiceActivity.java
com.davidparry.twitter.TweetList.java
com.davidparry.twitter.TweetPreferenceActivity.java
com.davidparry.twitter.TwitterPersistence.java
com.davidparry.twitter.YoutubeActivity.java
com.davidparry.twitter.analytics.ButlerAnalytics.java
com.davidparry.twitter.common.ActivityHelper.java
com.davidparry.twitter.common.Item.java
com.davidparry.twitter.common.Location.java
com.davidparry.twitter.common.ServicePreferences.java
com.davidparry.twitter.common.Tweet.java
com.davidparry.twitter.common.TwitterResult.java
com.davidparry.twitter.common.TwitterUser.java
com.davidparry.twitter.common.Util.java
com.davidparry.twitter.exception.QueryValidationException.java
com.davidparry.twitter.http.TwitterQueryRequest.java
com.davidparry.twitter.http.TwitterRequest.java
com.davidparry.twitter.listeners.ProfileImageLongClickListener.java
com.davidparry.twitter.listeners.buttons.ActionButtonDialogOnClickListener.java
com.davidparry.twitter.listeners.buttons.AdvancedSearchOnClickListener.java
com.davidparry.twitter.listeners.buttons.AuthorizeException.java
com.davidparry.twitter.listeners.buttons.BasicSearchOnClickListener.java
com.davidparry.twitter.listeners.buttons.ButtonPreference.java
com.davidparry.twitter.listeners.buttons.ListTweetsOnClickListener.java
com.davidparry.twitter.listeners.buttons.NextPageOnClickListener.java
com.davidparry.twitter.listeners.buttons.TweetActionButtonListener.java
com.davidparry.twitter.oath.TButlerOATH.java
com.davidparry.twitter.threads.ImageIconLoaderThread.java
com.davidparry.twitter.threads.ProfileUserThread.java
com.davidparry.twitter.threads.SDCardIOReadThread.java
com.davidparry.twitter.threads.SDCardIOWriteThread.java
com.davidparry.twitter.threads.StatusLongClickListener.java
com.davidparry.twitter.twitter4j.ButlerTwitterAdapter.java
com.davidparry.twitter.twitter4j.TwitterQuery.java
com.davidparry.twitter.widgets.ButlerDialog.java
com.davidparry.twitter.widgets.OptionMenu.java
com.davidparry.twitter.widgets.TwitterCheckBox.java