I created TwitterServices.java to handle the twitter services.

the reason why it was failing before is that i created a request token to get an auth url, and then i would create a new one when i get the auth code.
finished twitter intergration
This commit is contained in:
2011-01-31 02:45:04 +00:00
parent dbf1c509b2
commit c52f835f7a
4 changed files with 109 additions and 59 deletions

View File

@@ -0,0 +1,2 @@
I created TwitterServices.java to handle the twitter services.
the reason why it was failing before is that i created a request token to get an auth url, and then i would create a new one when i get the auth code.

View File

@@ -13,6 +13,7 @@ import android.content.Intent;
import android.location.Location;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.widget.Toast;
/**
@@ -46,13 +47,11 @@ public class LocationReceiver extends BroadcastReceiver{
private void onLocationUpdate(Location location) {
//TODO something with the location i.e. report location to social services like twitter, ect...
try {
TravelPost.tweet(location.toString());
Log.d(TravelPost.TAG, TwitterServices.tweet(mContext, location.getLatitude() +", "+ location.getLongitude() +" ± "+ location.getAccuracy()+"m").toString());
} catch (TwitterException e) {
Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG);
e.printStackTrace();
}
removeWakeLock();
}

View File

@@ -6,22 +6,14 @@
*/
package com.TwentyCodes.android.TravelPost;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.widget.Toast;
/**
* Main activity for the Travel Post widget.
@@ -31,15 +23,10 @@ import android.widget.Toast;
public class TravelPost extends PreferenceActivity implements OnPreferenceClickListener {
private PostMortemReportExceptionHandler mExceptionReport = new PostMortemReportExceptionHandler(this);
private Twitter twitter;
private static SharedPreferences shared_prefs;
private TwitterServices mTwitterServices = null;
public static final String SETTINGS = "settings";
private static final int TWITTER_AUTH_REQUEST_CODE = 0;
public static final String TWITTER_AUTH_TOKEN = "twitter_token";
public static final String TWITTER_AUTH_SECRET = "twitter_secret";
private static final String TC_TEST_OAUTH_KEY = "kmmcF8rjO4Aj9W4g8ao8ow";
private static final String TC_TEST_OAUTH_SECRET = "5pgeFWHd7dkde284ed7NsRuIVsyeOvioXR0tVxp9K8";
private static final String TAG = "TravelPost";
static final String TAG = "TravelPost";
/**
* called when the activity is first created
@@ -54,8 +41,6 @@ public class TravelPost extends PreferenceActivity implements OnPreferenceClickL
mExceptionReport.run();
Thread.setDefaultUncaughtExceptionHandler(mExceptionReport);
shared_prefs = getSharedPreferences(SETTINGS, 0);
// load preferences xml
this.addPreferencesFromResource(R.xml.settings);
@@ -64,13 +49,10 @@ public class TravelPost extends PreferenceActivity implements OnPreferenceClickL
@Override
public boolean onPreferenceClick(Preference preference) {
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(TC_TEST_OAUTH_KEY, TC_TEST_OAUTH_SECRET);
mTwitterServices = new TwitterServices(this);
try {
RequestToken requestToken = twitter.getOAuthRequestToken();
this.startActivityForResult(new Intent(this, WebAuth.class).putExtra(WebAuth.AUTH_URL, requestToken.getAuthorizationURL()), TWITTER_AUTH_REQUEST_CODE);
this.startActivityForResult(new Intent(this, WebAuth.class).putExtra(WebAuth.AUTH_URL, mTwitterServices.getAuthorizationURL()), TWITTER_AUTH_REQUEST_CODE);
} catch (TwitterException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG);
e.printStackTrace();
}
return false;
@@ -96,40 +78,12 @@ public class TravelPost extends PreferenceActivity implements OnPreferenceClickL
switch(requestCode){
case TWITTER_AUTH_REQUEST_CODE:
try {
Log.v(TAG,"twitter auth case");
AccessToken accessToken = null;
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(TC_TEST_OAUTH_KEY, TC_TEST_OAUTH_SECRET);
accessToken = twitter.getOAuthAccessToken(twitter.getOAuthRequestToken(), authCode);
Log.d(TAG, accessToken.getToken());
Log.d(TAG, accessToken.getTokenSecret());
Log.v(TAG,"saving twitter tokens");
shared_prefs = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);
shared_prefs.edit().putString(TWITTER_AUTH_TOKEN, accessToken.getToken()).commit();
shared_prefs.edit().putString(TWITTER_AUTH_SECRET, accessToken.getTokenSecret()).commit();
mTwitterServices.saveAuthorizationTokens(authCode);
} catch (TwitterException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG);
e.printStackTrace();
}
break;
}
}
/**
* posts a tweet to twitter
* @param msg
* @throws TwitterException
* @author ricky barrette
*/
public static void tweet(String msg) throws TwitterException{
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true);
cb.setOAuthConsumerKey(TWITTER_AUTH_TOKEN).setOAuthConsumerSecret(TWITTER_AUTH_SECRET);
cb.setOAuthAccessToken(shared_prefs.getString(TWITTER_AUTH_TOKEN, ""));
cb.setOAuthAccessTokenSecret(shared_prefs.getString(TWITTER_AUTH_SECRET, ""));
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
twitter.updateStatus(msg);
}
}

View File

@@ -0,0 +1,95 @@
/**
* TwitterServices.java
* @date Jan 30, 2011
* @author ricky barrette
* @author Twenty Codes, LLC
*/
package com.TwentyCodes.android.TravelPost;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
/**
* A convince class for working with the twitter4j API
* @author ricky barrette
*/
public class TwitterServices {
public static final String TWITTER_AUTH_TOKEN = "twitter_token";
public static final String TWITTER_AUTH_SECRET = "twitter_secret";
private static final String TC_TEST_OAUTH_KEY = "CFYYGtuYjEHmwycZIhYS3Q";
private static final String TC_TEST_OAUTH_SECRET = "Sf8nUTImllxET4SYriQ6e4YOLK4i8UUmO3x0tcPqhU";
private Twitter twitter;
private static SharedPreferences shared_prefs;
private RequestToken requestToken;
/**
* Posts a tweet to twitter. <p>
* You need to authorize your application first
* @param msg
* @throws TwitterException
* @author ricky barrette
* @return Status
*/
public static Status tweet(Context context, String msg) throws TwitterException{
shared_prefs = context.getSharedPreferences(TravelPost.SETTINGS, 0);
// Log.d(TravelPost.TAG, shared_prefs.getString(TWITTER_AUTH_TOKEN, ""));
// Log.d(TravelPost.TAG, shared_prefs.getString(TWITTER_AUTH_SECRET, ""));
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true);
cb.setOAuthConsumerKey(TC_TEST_OAUTH_KEY);
cb.setOAuthConsumerSecret(TC_TEST_OAUTH_SECRET);
cb.setOAuthAccessToken(shared_prefs.getString(TWITTER_AUTH_TOKEN, ""));
cb.setOAuthAccessTokenSecret(shared_prefs.getString(TWITTER_AUTH_SECRET, ""));
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
return twitter.updateStatus(msg);
}
/**
* Creates a new TwitterServices. <p>
* Initialization of this object is only needed for authorization, posting can be done statically once the users tokens are stored.
* @author ricky barrette
*/
public TwitterServices(Context context) {
shared_prefs = context.getSharedPreferences(TravelPost.SETTINGS, 0);
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(TC_TEST_OAUTH_KEY, TC_TEST_OAUTH_SECRET);
}
/**
* @return Authorization URL
* @throws TwitterException
* @author ricky barrette
*/
public String getAuthorizationURL() throws TwitterException{
requestToken = twitter.getOAuthRequestToken();
return requestToken.getAuthorizationURL();
}
/**
* Saves the authorization tokens for future use
* @param authorizationCode
* @return true if save was successful
* @throws TwitterException
* @author ricky barrette
*/
public boolean saveAuthorizationTokens(String authorizationCode) throws TwitterException{
AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, authorizationCode);
Log.v(TravelPost.TAG,"saving twitter tokens");
Editor e = shared_prefs.edit();
e.putString(TWITTER_AUTH_TOKEN, accessToken.getToken()).commit();
e.putString(TWITTER_AUTH_SECRET, accessToken.getTokenSecret()).commit();
return e.commit();
}
}