Android Facebook SDK 4.0 Login with LoginManager

I am trying to migrate the login code of an old application from SDK 3.0 to SDK 4.0. I logged in with LoginManager since I have custom login buttons.

The problem is that I am not getting a response from the Facebook API. There were no successes, mistakes, no exceptions. The code is as follows:

  //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 Debugger , I can see this line: mLoginMgr.logInWithReadPermissions(mActivity, mPermissions); executes, but none of the callbacks fires. I also have no errors in the console, and the device screen goes black and nothing happens.

I do not think this is important, but the code is executed in a wrapper class (outside of Activity ). I tried inside the Activity , but that didn't matter.

Any suggestions? Very 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" /> 
+7
android facebook-login
source share
2 answers

If your code is in a snippet, do not use the mActivity variable as an activity context.

To do:

  • Add line:

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

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

Use this , do not use this.getActivity ()

  1. Of course, done.
+10
source share

I also had this problem. Turns out I forgot to add the callbackManager.onActivityResult() call to Activity.onActivityResult() at the bottom of the Register Callback Section in the connection guide.

So, in the Office in which you make Facebook, 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 that what you were missing?

+4
source share

All Articles