2

I have the below code to log into Facebook and get the users email, this works fine when the user does not have the native app installed, it opens the popup and lets the user enter their facebook info. But when they have the native app it asks for the permissions and the does nothing more. Seems I have the same issue as How to disable facebook native app for user login as well as Android Facebook authorization - can not log in when official Facebook app is installed

However my code is quite a bit different so I was hope someone could help me force my code to only use the popup.

My code

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);
        }
    }

    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);
    }

UPDATE So I do not have an activity set in the Facebook dev settings, which makes sense, so what should I put to as the activity so that the FbLoginActivity processSessionStatus gets fired with the login (So I can get the email address and pass it in)

Community
  • 1
  • 1
DevWithZachary
  • 3,545
  • 11
  • 49
  • 101

4 Answers4

1

If you are using the facebook SDK, then this is the default behavior. If the user has the facebook app installed then it will use the credentials from that and if not, then it will show the popup. This is how it was designed to function since the user shouldn't have to enter his credentials again and again in every app that uses facebook for authentication. If you want to override this behavior, then you will have to fork and modify the SDK itself.

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
  • This shouldnt be the case see the two questions I linked – DevWithZachary Sep 13 '13 at 13:16
  • Is the app returned to correctly in the web sign up but not returning correctly from the native app? Then it is a potential problem with your Facebook app settings. It needs the full package and class name of the activity that is meant to be returned to. – Pork 'n' Bunny Sep 13 '13 at 13:23
0

Have you put into your Facebook App Settings on Facebook.com the name of the activity that the native facebook app is meant to go to once authentication has completed?

It's possible that it is missing or there is a mistake and results in the native facebook app calling a non-existent activity once the sign on is complete.

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
Pork 'n' Bunny
  • 6,740
  • 5
  • 25
  • 32
  • It shouldnt have to call anything surely? as the above code should just get the email from the session and launch the next activity – DevWithZachary Sep 13 '13 at 13:17
  • Is the app returned to correctly in the web sign up but not returning correctly from the native app? Then it is a potential problem with your Facebook app settings. It needs the full package and class name of the activity that is meant to be returned to. – Pork 'n' Bunny Sep 13 '13 at 13:24
  • Yes the app works fine with the web sign in pop up and native app does nothing. I have not got a activity class in the facebook dev settings so I guess this is the issue, but the question is how do I get it to return to the same activity and fire the processSessionStatus – DevWithZachary Sep 13 '13 at 13:26
  • fire processSessionStatus in onActivityResult – Pork 'n' Bunny Sep 13 '13 at 13:31
0
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    facebook.authorizeCallback(requestCode, resultCode, data);
}

Use this code in the activity where you call facebook login request.

atrivedi
  • 348
  • 1
  • 4
  • 12
0

the answer is: add this line in your cade

 session.openForRead(new Session.OpenRequest(
                        FbLoginActivity.this).setCallback(statusCallback)
                        .setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);// add this line for open only dialog or pop-up
                        .setPermissions(permissions));
Ando Masahashi
  • 3,112
  • 2
  • 24
  • 41