From 414f1a18bffd46b4a0ab58a77dd60e7d579027c8 Mon Sep 17 00:00:00 2001 From: warren powers Date: Sun, 6 Feb 2011 23:09:35 +0000 Subject: [PATCH] changed postToWall() to postWall() in FacebookAuth.java. created method postToWall(). this method should post without user interaction in FacebookAuth.java i splite the init() method into two. authorize() and logout(). created postToWallDialog() in FacebookAuth.java to display a dialog when posting to wall. created a custom exception called SessionNotValidException to be thrown when facebook hates us t seems the facebook sdk is not saving an access token when trying to make a direct facebook graph request. it does save the access token when calling a dialog but prompts for credentials. i believe the authorization process is failing. not sure why. still researching. --- TravelPost/ChangeLog.txt | 10 +++ .../android/Facebook/FacebookAuth.java | 73 ++++++++++++++++--- .../Facebook/SessionNotValidException.java | 16 ++++ .../android/TravelPost/TravelPost.java | 16 ++-- 4 files changed, 100 insertions(+), 15 deletions(-) create mode 100644 TravelPost/src/com/TwentyCodes/android/Facebook/SessionNotValidException.java diff --git a/TravelPost/ChangeLog.txt b/TravelPost/ChangeLog.txt index e69de29..0dd85b6 100644 --- a/TravelPost/ChangeLog.txt +++ b/TravelPost/ChangeLog.txt @@ -0,0 +1,10 @@ +changed postToWall() to postWall() in FacebookAuth.java. + +created method postToWall(). this method should post without user interaction + +in FacebookAuth.java i splite the init() method into two. authorize() and logout(). + +created postToWallDialog() in FacebookAuth.java to display a dialog when posting to wall. + +t seems the facebook sdk is not saving an access token when trying to make a direct facebook graph request. +it does save the access token when calling a dialog but prompts for credentials. i believe the authorization process is failing. not sure why. still researching. \ No newline at end of file diff --git a/TravelPost/src/com/TwentyCodes/android/Facebook/FacebookAuth.java b/TravelPost/src/com/TwentyCodes/android/Facebook/FacebookAuth.java index 4c60445..17cc19f 100644 --- a/TravelPost/src/com/TwentyCodes/android/Facebook/FacebookAuth.java +++ b/TravelPost/src/com/TwentyCodes/android/Facebook/FacebookAuth.java @@ -24,7 +24,7 @@ public class FacebookAuth { private static final String[] PERMISSIONS = new String[] {"publish_stream"}; /* Permissions that are prompted to the user for authorization */ - private static final String APP_ID = "160048987380834"; + private static final String APP_ID = "197267276952565"; /* This is the api key for the facebook application. See /FacebookLib/APIS.txt */ private static final String TAG = "FacebookAuth"; private Facebook mFb; @@ -57,23 +57,76 @@ public class FacebookAuth { public void init() { Log.i(TAG, "init()"); if (mFb.isSessionValid()) { - SessionEvents.onLogoutBegin(); - mAsyncRunner.logout(mCtx, new LogoutRequestListener()); + /* + *These lines log out you out of facebook. For now commented out. + */ +// SessionEvents.onLogoutBegin(); +// mAsyncRunner.logout(mCtx, new LogoutRequestListener()); } else { mFb.authorize(mActivity, PERMISSIONS, new LoginDialogListener()); } } /** - * This method posts a message to the wall - * @param message + * This method authorizes the facebook account + * @author warren */ - public void postToWall(String message) { - Log.i(TAG, "postToWall()"); - Bundle parameters = new Bundle(); - parameters.putString("message", message); - mFb.dialog(mCtx, "stream.publish", parameters, new FacebookDialogListener()); + public void authorize() { + if (!mFb.isSessionValid()) { + mFb.authorize(mActivity, PERMISSIONS, new LoginDialogListener()); + } + } + + /** + * This method starts the logout process + * @author warren + */ + + public void logout() { + if (mFb.isSessionValid()) { + SessionEvents.onLogoutBegin(); + mAsyncRunner.logout(mCtx, new LogoutRequestListener()); + } + } + + /** + * This method posts a message to the users wall + * @author warren + * @param msg - message to post + */ + + public void postToWall(String msg) throws SessionNotValidException { + Log.d(TAG, "Testing graph API wall post"); + if (mFb.isSessionValid()) { + try { + String response = mFb.request("me"); + Bundle parameters = new Bundle(); + parameters.putString("message", msg); + parameters.putString("description", "test test test"); + response = mFb.request("me/feed", parameters, "POST"); + Log.d(TAG, "got response: " + response); + if (response == null || response.equals("") || response.equals("false")) { + Log.v("Error", "Blank response"); + } + } catch(Exception e) { + e.printStackTrace(); + } + } else { + if (mFb.getAccessToken() == null) { + Log.i(TAG, "postToWall.access token is null"); + } + throw new SessionNotValidException("This session is not valid"); + } + } + + /** + * Allows the user to post to their wall and input their own message + * @author warren + */ + + public void postToWallDialog() { + mFb.dialog(mCtx, "feed", new FacebookDialogListener()); } /** diff --git a/TravelPost/src/com/TwentyCodes/android/Facebook/SessionNotValidException.java b/TravelPost/src/com/TwentyCodes/android/Facebook/SessionNotValidException.java new file mode 100644 index 0000000..289a417 --- /dev/null +++ b/TravelPost/src/com/TwentyCodes/android/Facebook/SessionNotValidException.java @@ -0,0 +1,16 @@ +package com.TwentyCodes.android.Facebook; + +import android.accounts.AccountsException; + +public class SessionNotValidException extends AccountsException { + + /** + * generated id + */ + private static final long serialVersionUID = 624797017013181185L; + + public SessionNotValidException(String message) { + new AccountsException(message); + } + +} diff --git a/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPost.java b/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPost.java index 2859217..f99a4c1 100644 --- a/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPost.java +++ b/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPost.java @@ -7,6 +7,7 @@ package com.TwentyCodes.android.TravelPost; import com.TwentyCodes.android.Facebook.FacebookAuth; +import com.TwentyCodes.android.Facebook.SessionNotValidException; import twitter4j.TwitterException; import android.app.Activity; @@ -28,7 +29,8 @@ public class TravelPost extends PreferenceActivity implements OnPreferenceClickL private TwitterServices mTwitterServices = null; public static final String SETTINGS = "settings"; private static final int TWITTER_AUTH_REQUEST_CODE = 0; - static final String TAG = "TravelPost"; + public static final String TAG = "TravelPost"; + private FacebookAuth mFbAuth; /** * called when the activity is first created @@ -48,6 +50,8 @@ public class TravelPost extends PreferenceActivity implements OnPreferenceClickL this.findPreference("twitter_sign_in").setOnPreferenceClickListener(this); this.findPreference("facebook_sign_in").setOnPreferenceClickListener(this); + mFbAuth = new FacebookAuth(this, this); + mFbAuth.authorize(); } @Override @@ -60,10 +64,12 @@ public class TravelPost extends PreferenceActivity implements OnPreferenceClickL e.printStackTrace(); } } else if (preference.getKey().equals("facebook_sign_in")) { - Log.i(TAG, "onPreferenceClick.Facebook sign in clicked"); - FacebookAuth auth = new FacebookAuth(this, this); - auth.init(); - auth.postToWall("Woot woot!"); + try { + mFbAuth.postToWall("Another test"); + } catch (SessionNotValidException e) { + e.printStackTrace(); + } + Log.i(TAG, "onPreferenceClick.posted to wall"); } return false;