I want to add the Facebook login feature to my application. Note:
- I do not want to use the Facebook login button widget.
- I copied my code that worked correctly last year (from my other project) into my new project.
- I checked Session and I think I have no problem.
The only difference: Request.executeMeRequestAsync()hss has been changed to Request.newMeRequest(). I made this change, but it seems that my session is always closed, although I could see that the method Session.setActiveSession(session);starts by debugging the project. So, I do not know why sessionit is always close in the method call().
Any idea would be appreciated. Thank.
My code is:
public class FacebookLogin extends FragmentActivity
{
private static final String TAG = "FacebookLogin";
private static final List<String> READ_PERMISSIONS =
Arrays.asList("email", "user_about_me", "user_photos");
private final Session.StatusCallback statusCallback = new Session.StatusCallback()
{
@Override
public void call(final Session session, SessionState state, Exception exception)
{
if (session.isOpened())
{
Request request = Request.newMeRequest(session, new Request.GraphUserCallback()
{
@Override
public void onCompleted(GraphUser user, Response response)
{
if (user != null)
{
MyLog.d(TAG, "User name: " + user.getName() + "!, Login successfully :)");
MyLog.d(TAG, "User id: " + user.getId());
MyLog.d(TAG, "Access token is: " + session.getAccessToken());
MyLog.d(TAG, "Application id: " + session.getApplicationId());
MyLog.d(TAG, "JSON Object: " + user.getInnerJSONObject());
SpStorage.setKeyFacebook(FacebookLogin.this, session.getAccessToken());
SpStorage.setFacebookUserId(FacebookLogin.this, user.getId());
FacebookLogin.this.finish();
}
}
});
request.executeAsync();
}
else if (state.isClosed()) {
MyLog.d(TAG, "Facebook session closed");
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
openActiveSession(this, true, statusCallback, READ_PERMISSIONS);
}
private static Session openActiveSession(Activity activity, boolean allowLoginUI,
Session.StatusCallback callback, List<String> permissions)
{
Session.OpenRequest openRequest = new Session
.OpenRequest(activity)
.setPermissions(permissions)
.setCallback(callback);
Session session = new Session.Builder(activity).build();
if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI)
{
Session.setActiveSession(session);
session.openForRead(openRequest);
return session;
}
return null;
}
}
What I get in logcat:
Facebook session closed