StatusCallback not called after requestNewReadPermissions then requestNewPublishPermissions - android

I am developing an Android App that integrates with Facebook. I would like to:
Let the user login with Facebook
Get the user's email address on Facebook (could be a proxied email address, which is fine)
Post to the user's wall/timeline on his/her behalf
Technically, that would be to:
Authenticate the user
Request the email permission
Request the publish_stream permission
1. Authenticate the user
I called Session.openActiveSession() with a Session.StatusCallback (I already checked that there is no active opened session beforehand):
final Session.StatusCallback sessionStatusCallback = new Session.StatusCallback() {
public void call(final Session session, SessionState state, Exception exception) {
// If there is an exception...
if(exception != null)
{
// Handle fail case here.
return;
}
// If session is just opened...
if(state == SessionState.OPENED)
{
// Handle success case here.
return;
}
};
};
// Start Facebook Login.
Session.openActiveSession(activity, true, sessionStatusCallback);
My callback is called after successful login. So far so good.
2. Request the email permission
This is my status callback:
new Session.StatusCallback() {
public void call(final Session session, SessionState state, Exception exception) {
// If there is an exception...
if(exception != null)
{
// Handle fail case here.
return;
}
// If token is just updated...
if(state == SessionState.OPENED_TOKEN_UPDATED)
{
// Handle success case here.
return;
}
};
};
I request the permission with Session.requestNewReadPermissions():
final Session session = Session.getActiveSession();
final static String[] PERMISSION_ARRAY_READ = {"email"};
final List<String> permissionList = Arrays.asList(PERMISSION_ARRAY_READ);
// If all required permissions are available...
if(session.getPermissions().containsAll(permissionList))
{
// Handle success case here.
return;
}
// Request permissions.
session.requestNewReadPermissions(new Session.NewPermissionsRequest(activity, permissionList));
My callback is called after permission is granted. So far so good.
3. Request the publish_stream permission
This is my status callback:
new Session.StatusCallback() {
public void call(final Session session, SessionState state, Exception exception) {
// If there is an exception...
if(exception != null)
{
// Handle fail case here.
return;
}
// If token is just updated...
if(state == SessionState.OPENED_TOKEN_UPDATED)
{
// Handle success case here.
return;
}
};
};
I request the permission with Session.requestNewPublishPermissions():
final Session session = Session.getActiveSession();
final static String[] PERMISSION_ARRAY_PUBLISH = {"publish_stream"};
final List<String> permissionList = Arrays.asList(PERMISSION_ARRAY_PUBLISH);
// If all required permissions are available...
if(session.getPermissions().containsAll(permissionList))
{
// Handle success case here.
return;
}
// Request permissions.
session.requestNewPublishPermissions(new Session.NewPermissionsRequest(activity, permissionList));
This time, my callback is not called after permission is granted.
Investigation
Upon further investigation, I found that my callback is triggered by com.facebook.Session#postStateChange(SessionState, SessionState, Exception):
void postStateChange(final SessionState oldState, final SessionState newState, final Exception exception) {
if (oldState == newState && exception == null) {
return;
}
/* ... */
}
Since oldState and newState are equal (both being SessionState.OPENED_TOKEN_UPDATED, my callback is not called.
Question
How can I receive any notification after permission is granted for the 2nd time? Am I supposed to close() the session and re-open it from cache?
Additional info
My Facebook Android SDK 3.0 is download from here, which is stated in Facebook's Getting Started with the Facebook SDK for Android.

This is a bug.
[edit: As Guy points out in comments, this was fixed in 3.0.1, so this workaround is no longer necessary]
The workaround you mention is basically correct, though you do not need to call close. If you are using the single active session, before calling requestNewPublishPermissions() just call:
Session.openActiveSessionFromCache(myContext);
If you are using multiple sessions, you need to initialize a new Session with the TokenCachingStrategy, verify it is in the CREATED_TOKEN_LOADED state, and call openForRead(null);
After doing one of these, requestNewPublishPermissions() should call your notification once it completes.

Working code based on rightparen's answer
Before requesting permission for the 2nd time (i.e. before Session.requestNewPublishPermissions()), do this:
// Re-initialize Facebook session.
session.removeCallback(sessionStatusCallback); // Remove callback from old session.
session = Session.openActiveSessionFromCache(context); // Create new session by re-opening from cache.
session.addCallback(sessionStatusCallback); // Add callback to new session.
Code is still based on Facebook Android SDK 3.0, as in the question.

Another thing I ran into was that my requestCode for the NewPermissionRequest was not being set to the same requestCode that I used to open my Session for Read with, thereby my Session.StatusCallback was never being invoked when the new permissions have been granted.
For instance, in my onActivityResult I have a check for the requestCode and delegate the call accordingly because I have other stuff coming in to this method.
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
if (requestCode == FACEBOOK_AUTH_RESULT_CODE) {
Session session = Session.getActiveSession();
if(session != null) {
session.onActivityResult(activity, requestCode, resultCode, data);
}
}
}
I then opened my Session with the following code :
Session.getActiveSession().openForRead(
new Session.OpenRequest(activity).
setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK).
setRequestCode(FACEBOOK_AUTH_RESULT_CODE).
setPermissions(MY_READ_PERMISSIONS));
I then forgot to use the same requestCode when constructing my NewPermissionRequest.
This is what the correct NewPermissionRequest needs to look like :
Session.getActiveSession().requestNewPublishPermissions(
new NewPermissionsRequest(activity, MY_PUBLISH_PERMISSIONS)
.setRequestCode(FACEBOOK_AUTH_RESULT_CODE));

Related

Ask for additional facebook permission

I'm having trouble while trying to retrieve an additional permission "publish_action".
I assume that user has already logged in via facebook thus basic permissions are granted.
Then I have this code (from my fragment):
List<String> permissionsRequested = Arrays.asList("publish_actions");
List<String> permissionsActual = session.getPermissions();
if (!isSubsetOf(permissionsRequested, permissionsActual)) {
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(this, permissionsRequested);
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
Seeing from debug, after user grants permission, control goes to onActivityResult, where, according to facebook docs, I have
super.onActivityResult(requestCode, resultCode, data);
facebookUiHelper.onActivityResult(requestCode, resultCode, data);
uiHelper is also properly (acording to the docs) used in all those onResume&onDestroy methods.
In onCreate I have
super.onCreate(savedInstanceState);
facebookUiHelper = new UiLifecycleHelper(getActivity(), fbSessionCallback);
facebookUiHelper.onCreate(savedInstanceState);
where fbSessionCallback has overridden call() method, that calls my onFbSessionRestore():
if (pendingPublishReauthorization &&
state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
pendingPublishReauthorization = false;
// do things with my new session, updated with granted permission. e.g. post to fb.
}
Trouble is that
onActivityResult executes, but after that no callback ever called, so that overridden call() won't go, and onFbSessionCallback either.
If I try to post again (start things from scratch) after that - it gets session without granted permission again, tries to request it and then goes facebook exception about pending newPublishRequest permission blah-blah
Can someone help to configure workflow from onActivityResult and later?
I read facebook docs and I tend to blame they are written badly. Maybe it's just me too stupid... :(((
This is how I have set it up:
Session session = Session.getActiveSession();
if (session != null) {
List<String> permissions = session.getPermissions();
if (!permissions.contains("publish_actions")) {
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, "publish_actions");
session.addCallback(callbacktwo);
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
}
This is in my onActivityResult but it doesn't need to be. I have used it in my onCreate before. I added a callback to the request which I can then use to do my next step.
Here is my callback:
private Session.StatusCallback callbacktwo = new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
Intent in = new Intent(getActivity(), ClassHere.class);
startActivity(in);
}
};
Remember that the callback will respond twice, once when you start a permission request, and after it is completed. So you have to work around that/work that into your code.

Requesting new permissions fail handling

Login and asking permission works just fine. But there is one problem: I need to ask publish permission when user wants to share some date from my app. Here is my code:
ParseFacebookUtils.getSession().requestNewPublishPermissions(new NewPermissionsRequest((Activity) context,
Arrays.asList(Permissions.Extended.PUBLISH_ACTIONS, Permissions.Extended.PUBLISH_STREAM))
.setCallback(new StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
if (!arePublishPermissionsEnabled(session)) {
inflow.setChecked(false);
facebook.setChecked(false);
twitter.setChecked(false);
}
}
}));
The problem is in handling situation when user canceled request or there is a loss of network connection. In this case I need to do some changes in my UI, but method call is calling only when session state is changed (e.g. granted new permissions) and I can't properly changed my UI. Is anyone faced such problem?
private void requestPublishPermissions(Session session) {
List<String> PERMISSIONS = Arrays.asList("publish_actions", "publish_stream");
if (session != null) {
pendingAnnounce = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, PERMISSIONS);
newPermissionsRequest.setRequestCode(REAUTH_ACTIVITY_CODE);
Session mSession = Session.openActiveSessionFromCache(this);
mSession.addCallback(callback);
mSession.requestNewPublishPermissions(newPermissionsRequest);
}
}

Android facebook sdk 3.0.1 using last session

I have the following scenario with facebook sdk 3.0.1. When user first login and chooses "FB login" then the SSO starts, a new session is open and everything works fine. But then, when the user closes the app and start it again - I don't understand how to get the last open session, currently I'm opening a new session and the user sees again the FB progress bar(while it's being connected to FB again, even so the user already approved FB in his last run). Does anybody know how to skip this operation?
Edit 1:
This is how I retrieve the session:
public void tryRetrievFacebookSession() {
Session session = Session.getActiveSession();
if (session != null && session.isOpened())
return;
session = Session.openActiveSession(this, true, new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
MobliLog.d("SplashScreen", "Inside call() with session with state: " + session.getState());
// onSessionStateChanged(session, state, exception);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session session = Session.getActiveSession();
if (session != null)
session.onActivityResult(this, requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
The session is normally being created or from the LoginButton or with those lines:
session = Session.getActiveSession();
if (session.getState().isClosed())
session = new Session(this);
if (session.isOpened()) {
onAuthenticationEndListener.onSuccessfullAuthentication();
return;
} else {
this.onFacebookAuthenticationEndListener = onAuthenticationEndListener;
Session.setActiveSession(session);
session.openForRead(new Session.OpenRequest(SocNetwksCompatScreen.this).setCallback(null));
return;
}
Information 1:
When I'm doing the first Session session = Session.getActiveSession(); in the logins after the sso authentication, my session has state CLOSED instead of OPENED
Information 2:
I'm using uiHelper and initialize it like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
twitter = SocialPluginsUtils.getTwitterClient();
uiHelper = new UiLifecycleHelper(this, statusCallback);
uiHelper.onCreate(savedInstanceState);
}
Now, I know that after I perform Session.getActiveSession(); the session state should get OPENED and after that OPEN and then the uiHelper's callback should be invoked. In my code the state is CLOSED or CLOSED_LOGIN_FAILED or CREATED(not sure why it's not stable) and the rest doesn't happens
In fact the sessions are being closed every time the user closed the app.
So if Session.getActiveSession() return a null session you only need to call Session.openActiveSession(activity, true, sessionCallback).
If there is a valid token cache this method will use it in order to open a new session without the need for the user to insert any data. Otherwise this will shows the default dialog with the basic permissions.
From what I can see after your edit maybe the problem is related with the way in which you manage the session.
I use this code in my projects:
Session session = Session.getActiveSession();
if (session == null){
Session.openActiveSession(activity, true, sessionCallback);
}
else if (!session.getState().isOpened()){
session.openForRead(new Session.OpenRequest(activity)
.setCallback(sessionCallback));
//this will open the session with basic read permissions
}
else {
//do what you want with the opened session
}
Moreover if you use the UiLifecycleHelper you don't need the two line of code that I suggest to you in the comment, they are already in the method of the helper. But you must be sure to call all the methods of the helper in each related method of the activity (onResume, onPause etc.)
If there isn't a token cache the openActiveSession(activity, true, sessionCallback) will automatically call a new dialog and if the user login with success a new token cache will be available for future uses.
Problem solved. I accidentally called session.closeAndClearTokenInformation(); in the onStop() (yeah, so stupid!)

Facebook Android SDK Session openForPublish not creating a new session

In the Facebook Android SDK when I call
Session tempSession = new Builder(this).build();
Session.setActiveSession(tempSession);
tempSession.openForRead(new OpenRequest(this).setPermissions(FB_PERMISSIONS));
It gets a FB session and every thing runs as normal. But when I replace Read with Publish. i.e. follows
Session tempSession = new Builder(this).build();
Session.setActiveSession(tempSession);
tempSession.openForPublish(new OpenRequest(this).setPermissions(FB_PERMISSIONS));
It gives an error saying, that the session is empty, and cannot get publish permissions to empty session.
Can you please tell why is it like this and what would be the best way to handle this?
It took me a while to sort this out so a user could click a button to share one of my products on Facebook in a feed. I didn't want them to be prompted to sign in until they actually wanted to share, so I really just wanted publish permission. The following stacks the initial login/read permission request with the publish permission request. This will double-prompt the users, first for read, then for publish, but that is required now regardless of the solution:
Session session = Session.getActiveSession();
if (session == null) {
session = new Session.Builder(this).setApplicationId("<APP ID HERE>").build();
Session.setActiveSession(session);
session.addCallback(new StatusCallback() {
public void call(Session session, SessionState state, Exception exception) {
if (state == SessionState.OPENED) {
Session.OpenRequest openRequest = new Session.OpenRequest(FacebookActivity.this);
openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
session.requestNewPublishPermissions(
new Session.NewPermissionsRequest(FacebookActivity.this, PERMISSIONS));
}
else if (state == SessionState.OPENED_TOKEN_UPDATED) {
publishSomething();
}
else if (state == SessionState.CLOSED_LOGIN_FAILED) {
session.closeAndClearTokenInformation();
// Possibly finish the activity
}
else if (state == SessionState.CLOSED) {
session.close();
// Possibly finish the activity
}
}});
}
if (!session.isOpened()) {
Session.OpenRequest openRequest = new Session.OpenRequest(this);
openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
session.openForRead(openRequest);
}
else
publishSomething();
The short answer is, don't call openForPublish. Call openForRead, and then requestNewPublishPermissions later if you need publish permissions.
The long answer is, you can't request publish permissions (on a user who's never connected with Facebook before via your app) unless you already have basic or default permissions already (what you would get if you call openForRead with an empty permission set). So openForPublish actually handles a very specific niche use case that most apps probably don't have.

User is asked for login credentials during reauthorization

I am trying to implement the new Facebook SDK 3.0 into my Android app and I have run into a problem. The problem I am having is that the user is asked to log in again to give publish ("post_to_wall") permissions, even though the user is already logged in, with read permissions. This only happens if the user doesn't have the FB application installed. If he has the FB application installed, then he is only asked to grant the permissions.
This is how I implemented the login:
public void login(Activity activity) {
Session session = Session.getActiveSession();
if (session == null || !session.isOpened()) {
openActiveSession(activity, true, sessionStatusCallback);
}
}
private Session openActiveSession(final Activity activity, final boolean allowLoginUI, final StatusCallback callback) {
return openActiveSession(activity, allowLoginUI, new OpenRequest(activity).setCallback(callback));
}
private Session openActiveSession(final Context context, final boolean allowLoginUI, final OpenRequest openRequest) {
Session session = new Builder(context).setApplicationId(FACEBOOK_APPLICATION_ID).build();
if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI) {
Session.setActiveSession(session);
session.openForRead(openRequest);
return session;
}
return null;
}
This is the callback's call method:
public void call(final Session session, final SessionState state, final Exception exception) {
if (session.isOpened()) {
if (state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
// code if new permissions have been granted
} else {
// code for login
}
} else if (session.isClosed()) {
// code for user canceled login
} else if (exception != null) {
// code if there were errors during login
}
}
}
This is the code I added to onActivityResult method of the activity that calls the login:
Session.getActiveSession().onActivityResult(activity, requestCode, resultCode, data);
And this is how I ask for new permissions:
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
if (DONT_HAVE_PERMISSIONS) {
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(activity,
FACEBOOK_PERMISSIONS).setRequestCode(FACEBOOK_AUTHORIZE_ACTIVITY_CODE);
session.requestNewPublishPermissions(newPermissionsRequest);
}
}
I've tried to find out more about this problem, and I only found out some hints that this is intended, but I haven't found anything concrete.
Is this the default behavior? If so, is there a way around it? Or, perhaps I did something wrong?
Thanks for the help.
Update your SDK version; this issue is resolved in Facebook Android SDK v3.0.1.
Looking at facebooks source code I think it should be possible to start trying to get the permissions directly as both login, and permissions classes derive from the same AuthorizationRequest class, and the AuthorizationRequest class does all the work, like really all the work. The Session.NewPermissionsRequest class just makes some private methods, public in the AuthorizationRequest class and that's it! They might as well give us access to AuthorizationRequest directly. The new facebook API doesn't seem to have any form of "OnFailed/OnSuccess" callbacks, so I end up having a state machine to remember the goal of firing up facebook (login, permissions, get friends list ...), and which step I'm on. If they have done some form of onFailed/onSuccess callbacks it would be simple to make a chain rather than keeping track of a state machine.
I haven't tried what I said. If I do, I'll update the answer. If you try and it works to just fire up Session.NewPermissionsRequest directly without logging in let me know!
Update
I got it working with only asking for credentials once as I explained above.
goto src/com/facebook/Sessions.java
On line 862 you will find
private static Session openActiveSession(Context context, boolean allowLoginUI, OpenRequest openRequest)
make it be a public function and save.
Now instead of creating the Session.NewPermissionsRequest object. Make Session.OpenRequest
permisions = new ArrayList<String>();
permisions.add("user_photos");
permisions.add("friends_photos");
Session.NewPermissionsRequest request = new Session.NewPermissionsRequest(
activity, permisions);
request.setCallback(helper);
if (Session.getActiveSession() != null)
Session.getActiveSession().requestNewReadPermissions(request);
else {
Session.OpenRequest orequest = new Session.OpenRequest(activity);
orequest.setPermissions(permisions);
orequest.setCallback(helper);
// its now public so you can call it
Session.openActiveSession(act, true, request);
}
Now make sure you do set a callback, for one important reason
#Override
public void call(Session session, SessionState state, Exception exception) {
if (state.isClosed()) {
// we are doing unofficial stuff so we loose guarantees.
// Set the active session to null if we logout or user cancels
// logging in. If you don't do this, the second time it will result
// in a crash.
Session.setActiveSession(null);
}
}
Now it will ask for all permissions directly and login in one go.

Categories

Resources