Single sign on works on emulator but not on device

2019-08-26 01:36发布

问题:

I'm having a very annoying problem using Facebook's sign-on. It works perfectly fine when running on an emulator, but when trying to use it on an actual device causes the following to happen: I see the facebook page loading, for a few seconds, and then nothing.

_facebook = new Facebook(Common.APP_ID);
_facebook.authorize(this, new DialogListener() {
    @Override
    public void onComplete(Bundle values) {
        try {
            JSONObject json = new JSONObject(_facebook.request("me"));
            String id = json.getString("id");
            String token = _facebook.getAccessToken();
            Log.w("facebook", id);
            Log.w("facebook", token);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    @Override
    public void onFacebookError(FacebookError error) {
        Log.w("facebook", "onFacebookError");
    }
    @Override
    public void onError(DialogError e) {
        Log.w("facebook", "onError");
    }
    @Override
    public void onCancel() {
        Log.w("facebook", "onCancel");
    }
});

I get no logs saying anything is wrong, it does not stop at any of the onError methods, nothing. Just carries on as usual. I've followed everything written online, I've added the native app ID gotten using the keytool to the app page, and still - NADA.

Any help (even a workaround) would be greatly appriciated! Thanks

回答1:

Since @harism isn't posting his comment as an answer, I figured I would at least to have an answer on this question.

What I missed was having this piece of code

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    _facebook.authorizeCallback(requestCode, resultCode, data);
}

Why it would on an emulator without it but not on an actual device is something I'm not entirely sure, but using this code does cause the SSO to work on my device.



回答2:

As a workaround you can just force facebook to use Facebook.FORCE_DIALOG_AUTH. I'm having the ame problem at the moment and this is the only thing I could find that works :/



回答3:

You need to get the Android hash key using the command below and put it in your facebook app configuration at developer.facebook.com/apps

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

For more info, here is a really useful blog post: http://sean.lyn.ch/2011/07/android-the-facebook-sdk-sso-and-you/