Вопрос

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)

Это было полезно?

Решение 2

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.

Другие советы

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.

@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.

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));
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top