Android Facebook SDK 4.0 login with LoginManager

nightfixed picture nightfixed · Apr 29, 2015 · Viewed 11k times · Source

I am trying to migrate login code of an old application from SDK 3.0 to SDK 4.0. I have implemented the Login using the LoginManager, as I have custom Login buttons.

The problem is that I get no response from the Facebook API. No success, no error, no exception thrown whatsoever. The code is as below:

    //global refs
    //callbacks
    private CallbackManager mCallbackManager;
    private FacebookCallback<LoginResult> mFacebookCallback;

    private List<String> mPermissions = Arrays.asList("email");
    private LoginManager mLoginMgr;
    private Activity mActivity;

    //........

    //code is inside method
    FacebookSdk.sdkInitialize(getApplicationContext());

    //perhaps a bit excessive
    FacebookSdk.addLoggingBehavior(LoggingBehavior.GRAPH_API_DEBUG_INFO);
    FacebookSdk.addLoggingBehavior(LoggingBehavior.DEVELOPER_ERRORS);
    FacebookSdk.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
    FacebookSdk.addLoggingBehavior(LoggingBehavior.INCLUDE_RAW_RESPONSES);
    FacebookSdk.setApplicationId(mActivity.getString(R.string.sample_fb_id));

    //init callbacks
    mFacebookCallback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.v("LoginActivity login", loginResult.toString());
            GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject object, GraphResponse response) {
                    // Application code
                    Log.v("LoginActivity", response.toString());
                    try {
                        String email = object.getString("email");
                        Log.v("LoginActivity", "obtained email: ", email);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
            request.executeAsync();
        }
        @Override
        public void onCancel() {
            Log.e("LoginActivity", "facebook login canceled");
        }

        @Override
        public void onError(FacebookException e) {
            Log.e("LoginActivity", "facebook login failed error");
        }
    };
    mCallbackManager = CallbackManager.Factory.create();
    mLoginMgr = LoginManager.getInstance();
    mLoginMgr.registerCallback(mCallbackManager, mFacebookCallback);
    mLoginMgr.logInWithReadPermissions(mActivity, mPermissions);

Using the Debugger, I am able to see that this line: mLoginMgr.logInWithReadPermissions(mActivity, mPermissions); gets executed, but none of the callbacks are ever triggered. I also have no errors in console, and the Device screen goes black and nothing happens.

I don't think it matters, but the code is executed in a wrapper class (outside Activity). I tried inside the Activity, but it made no difference.

Any suggestions? Much appreciated.

Edit:

This is declared inside AndroidManifest.xml:

<activity android:name="com.facebook.FacebookActivity"
          android:theme="@android:style/Theme.Translucent.NoTitleBar"
          android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
          android:label="@string/app_name" />

Answer

FIT226557 picture FIT226557 · May 18, 2015

If your code is in a fragment, do not use mActivity variable as Activity context.

To do:

  1. Add a line:

     callbackManager.onActivityResult(requestCode, resultCode, data); in onActivityResult(, , , ) in fragment.
    
  2. Call:

     LoginManager.getInstance().logInWithReadPermissions(**this**, Arrays.asList("public_profile", "user_friends")); 
    

Use this, not use this.getActivity()

  1. Sure, Done.