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.
This commit is contained in:
@@ -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.
|
||||
@@ -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()");
|
||||
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", message);
|
||||
mFb.dialog(mCtx, "stream.publish", parameters, new FacebookDialogListener());
|
||||
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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user