0

I have the below code which is used to allow users to login with facebook. Currently if the user does not have the native android facebook app installed and so the webview popup login i used everything works fine, the user is logged in and the processSessionStatus() is fired.

This passes the users email on to my apps own login.

The issue is when the user has the native app installed and they use that for the facebook login, the processSessionStatus() does not fire.

I have set up my facebook dev settings to include the FbLoginActivity as the activity to be used by the native login. I guess I need to add something to onActivityResult to fire processSessionStatus() once the user has logged in with the native app?

However I am not sure what I need to pass to get it to fire correctly.

My FbLoginActivity:

public class FbLoginActivity extends Activity {

    private static List<String> permissions;
    Session.StatusCallback statusCallback = new SessionStatusCallback();
    ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fblogin);
        Button fbButton = (Button) findViewById(R.id.fbshare);
        /***** FB Permissions *****/
        permissions = new ArrayList<String>();
        permissions.add("email");
        /***** End FB Permissions *****/
        fbButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Check if there is any Active Session, otherwise Open New
                // Session
                Session session = Session.getActiveSession();
                if (session == null) {
                    Session.openActiveSession(FbLoginActivity.this, true,
                            statusCallback);
                } else if (!session.isOpened()) {
                    session.openForRead(new Session.OpenRequest(
                            FbLoginActivity.this).setCallback(statusCallback)
                            .setPermissions(permissions));
                }
            }
        });
        Session session = Session.getActiveSession();
        if (session == null) {
            if (savedInstanceState != null) {
                session = Session.restoreSession(this, null, statusCallback,
                        savedInstanceState);
            }
            if (session == null) {
                session = new Session(this);
            }
            Session.setActiveSession(session);
            session.addCallback(statusCallback);
            if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
                session.openForRead(new Session.OpenRequest(this).setCallback(
                        statusCallback).setPermissions(permissions));
            }
        }
    }

    private class SessionStatusCallback implements Session.StatusCallback {

        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
            // Check if Session is Opened or not
            processSessionStatus(session, state, exception);
        }
    }

    @SuppressWarnings("deprecation")
    public void processSessionStatus(Session session, SessionState state,
            Exception exception) {
        if (session != null && session.isOpened()) {
            if (session.getPermissions().contains("email")) {
                // Show Progress Dialog
                dialog = new ProgressDialog(FbLoginActivity.this);
                dialog.setMessage("Logging in..");
                dialog.show();
                Request.executeMeRequestAsync(session,
                        new Request.GraphUserCallback() {

                            @Override
                            public void onCompleted(GraphUser user,
                                    Response response) {

                                if (dialog != null && dialog.isShowing()) {
                                    dialog.dismiss();
                                }
                                if (user != null) {
                                    Map<String, Object> responseMap = new HashMap<String, Object>();
                                    GraphObject graphObject = response
                                            .getGraphObject();
                                    responseMap = graphObject.asMap();
                                    Log.i("FbLogin", "Response Map KeySet - "
                                            + responseMap.keySet());
                                    // TODO : Get Email
                                    // responseMap.get("email");
                                    String fb_id = user.getId();
                                    String email = null;
                                    String name = (String) responseMap
                                            .get("name");
                                    if (responseMap.get("email") != null) {
                                        email = responseMap.get("email")
                                                .toString();
                                        Intent i = new Intent(FbLoginActivity.this, FbLogin2Activity.class);
                                        i.putExtra("Email", email);
                                        startActivity(i);
                                    } else {
                                        // Clear all session info & ask user to
                                        // login again
                                        Session session = Session
                                                .getActiveSession();
                                        if (session != null) {
                                            session.closeAndClearTokenInformation();
                                        }
                                    }
                                }
                            }
                        });
            } else {
                session.requestNewReadPermissions(new Session.NewPermissionsRequest(
                        FbLoginActivity.this, permissions));
            }
        }
    }

    /********** Activity Methods **********/
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("FbLogin", "Result Code is - " + resultCode + "");
        Session.getActiveSession().onActivityResult(FbLoginActivity.this,
                requestCode, resultCode, data);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Save current session
        super.onSaveInstanceState(outState);
        Session session = Session.getActiveSession();
        Session.saveSession(session, outState);
    }

    @Override
    protected void onStart() {
        // TODO Add status callback
        super.onStart();
        Session.getActiveSession().addCallback(statusCallback);
    }

    @Override
    protected void onStop() {
        // TODO Remove callback
        super.onStop();
        Session.getActiveSession().removeCallback(statusCallback);
    }
} 
DevWithZachary
  • 3,545
  • 11
  • 49
  • 101
  • So user is not able to login? – Vishal Pawale Sep 14 '13 at 15:23
  • @VishalPawale User logins fine however when they have the native app and log in with that it just returns to the FbLoginActivity and doesn’t fire processSessionStatus() while when they log in with the webview popup processSessionStatus() gets fired fine – DevWithZachary Sep 14 '13 at 15:36
  • So in case of native app, you dont get emailId of user? – Vishal Pawale Sep 14 '13 at 15:55
  • @VishalPawale Well it does not even get that far, you login it is logged in but processSessionStatus() does not get run so the email etc does not happen – DevWithZachary Sep 14 '13 at 15:58
  • Ohh okay..so the problem is if user is already logged in, `processSessionStatus()` is not getting called on clicking `fbButton`.I will suggest to have a look on `fbButton`'s click listener, it does nothing when user is logged in.Actually it is supposed to keep track of whether user is logged in or not by activity, using `Preferences`.So what you can do is- Keep boolean is prefs & Once user logs in mark it to true & store his email in preferences so that user dont need to login again & again.And each time user launches your app, check if user is already logged in or not. – Vishal Pawale Sep 14 '13 at 16:19

1 Answers1

0

I had a similar issue. It turns out that Facebook really wants you to separate your requests in "batches" when using SSO. From the Facebook docs (as seen on the following link),

You should plan your app to request minimum read permissions upon install.

this is what I found out when I was looking into this.

Community
  • 1
  • 1
Emmanuel
  • 13,083
  • 4
  • 39
  • 53