Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Facebook SDK 4.0 login with LoginManager

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" />
like image 751
nightfixed Avatar asked Apr 29 '15 13:04

nightfixed


2 Answers

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.
like image 129
FIT226557 Avatar answered Nov 20 '22 07:11

FIT226557


I had this problem also. Turns out I forgot to add the call to callbackManager.onActivityResult() in Activity.onActivityResult() as per the bottom of the Register a Callback section of the login guide.

So in the Activity in which you're doing Facebook login, you should have something like:

@Override
protected void onActivityResult(int request, int result, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    mCallbackManager.onActivityResult(request, result, data);
}

Is this what you were missing?

like image 4
RuideraJ Avatar answered Nov 20 '22 07:11

RuideraJ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!