10

I need to log in to Facebook and get same fields like email, etc. I use the Facebook SDK, and I set my Android key Hash in developers.facebook and set "Configured for Android SSO". In the simulator and some devices the application works fine.

But if the official Facebook application is installed on the device, my application does not work: I push the login button, but I not see a dialog with a web-view were my password and login are asked for. It looks like the problem in Stack Overflow question Using facebook.authorize with the Android SDK does not call onActivityResult or Stack Overflow question Android Facebook API single sign-on?, but I can not understand how to resolve it.

My code

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    this.facebookConnector.getFacebook().authorizeCallback(requestCode, resultCode, data);
}

public void getAccessToken() {
    SessionEvents.AuthListener listener = new SessionEvents.AuthListener() {
        @Override
        public void onAuthSucceed() {
            setupAccessToken(facebookConnector.getFacebook().getAccessToken());
        }

        @Override
        public void onAuthFail(String error) {
            Toast.makeText(getApplicationContext(), getString(R.string.error_login), Toast.LENGTH_SHORT).show();
        }
    };

    SessionEvents.addAuthListener(listener);
    facebookConnector.login();
}

facebookConnector code

public class FacebookConnector {
    public void login() {
        if (!facebook.isSessionValid()) {
            facebook.authorize(this.activity, this.permissions, new LoginDialogListener());
        }
    }

    private final class LoginDialogListener implements DialogListener {
        public void onComplete(Bundle values) {
            SessionEvents.onLoginSuccess();
        }

        public void onFacebookError(FacebookError error) {
            SessionEvents.onLoginError(error.getMessage());
        }

        public void onError(DialogError error) {
            SessionEvents.onLoginError(error.getMessage());
        }

        public void onCancel() {
            SessionEvents.onLoginError("Action Canceled");
        }
    }
}
Community
  • 1
  • 1
anber
  • 3,463
  • 6
  • 39
  • 68
  • if you facebook login integrations i working can you please check this and tell me where i am going wrong: http://stackoverflow.com/questions/11293815/how-to-ignore-facebook-android-application-installed-on-the-device-when-using-a ....or please paste the working code of your app – Archie.bpgc Jul 02 '12 at 14:12

4 Answers4

20

Please update the below code of your application. It will solve your problem.

public void loginAndPostToWall() {
    facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH,
            new LoginDialogListener());
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
  • if we change the Facebook password and check request to "me", if there is some error then open facebook login dailog , but it shows error again if we check for me request , why session token did not replaced? – SRam Aug 08 '13 at 10:08
  • But Dipak do you think think this is a fool proof solution for this/ Shouldn't the sdk get the session from the facebook app installed on the device? – Nitesh Verma Aug 12 '14 at 08:41
5

I had the same problem like you. Finally, I solved using this:

Open Facebook.java provided by the Facebook SDK and then change it like this:

public void authorize(Activity activity, String[] permissions,
                      int activityCode, final DialogListener listener) {
    boolean singleSignOnStarted = false;

    mAuthDialogListener = listener;

    /*
    // Prefer single sign-on, where available.
    if (activityCode >= 0) {
        singleSignOnStarted = startSingleSignOn(activity, mAppId,
                                                permissions, activityCode);
    }
    // Otherwise fall back to the traditional dialog.
    if (!singleSignOnStarted) {
    */

    startDialogAuth(activity, permissions);

    // }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dhruvil Patel
  • 2,910
  • 2
  • 22
  • 39
  • There's no need to change the facebook code, things should work without hacking it. It's not good practice since facebook can change their code tomorrow and then when you update your own codebase you need to remember all changes you had made. – Nitzan Tomer May 30 '12 at 12:12
  • Thanks for the answer, it was useful, but @Dipak Keshariya code do the same in more correct way. – anber May 30 '12 at 13:25
1

This is just a wild guess, but instead of this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    this.facebookConnector.getFacebook().authorizeCallback(requestCode, resultCode, data);
}

Try:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    this.facebookConnector.getFacebook().authorizeCallback(requestCode, resultCode, data);
}

Since you're not calling the parent method some things might not work as expected...

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
0
private static Session openActiveSession(Activity activity, boolean allowLoginUI, StatusCallback callback, List<String> permissions) {
    OpenRequest openRequest = new OpenRequest(activity).setPermissions(permissions).setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO).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;
}

Edit your openactivesession function like this

Jossy Paul
  • 1,267
  • 14
  • 26