login Facebook registerCallback not called

2020-08-09 06:53发布

Here is my onCreate method:

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.activity_social_login);
    init();
    hideActiveSocialNetworks();
    FacebookSdk.sdkInitialize(getApplicationContext());
    CallbackManager callbackManager = CallbackManager.Factory.create();
    LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            // not called
            Log.d("fb_login_sdk", "callback success");
        }

        @Override
        public void onCancel() {
            // not called
            Log.d("fb_login_sdk", "callback cancel");
        }

        @Override
        public void onError(FacebookException e) {
            // not called
            Log.d("fb_login_sdk", "callback onError");
        }
    });

    final Activity activity = this;

    findViewById(R.id.fb_login_sdk).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d("fb_login_sdk", "click");
            List<String> perm = new ArrayList<String>();
            perm.add("user_friends");
            LoginManager.getInstance().logInWithReadPermissions(activity, perm);
        }
    });
}

After login the onSuccess(), onCancel(), onError() methods are not fired.

Documentation: https://developers.facebook.com/docs/facebook-login/android/v2.3

3条回答
地球回转人心会变
2楼-- · 2020-08-09 07:43

Your code will work when you add this to your activity class

 @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    } 
查看更多
趁早两清
3楼-- · 2020-08-09 07:45

Missing this on my activity:

   @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
查看更多
Luminary・发光体
4楼-- · 2020-08-09 07:51

Here is checklist to check whether your Facebook Sdk setup is correct:-

  1. Check your manifest if you have setup Facebook initialization properly.

    <meta-data android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id" />
    
    <activity android:name="com.facebook.FacebookActivity"
        android:configChanges=
                "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name" />
    <activity
        android:name="com.facebook.CustomTabActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="@string/fb_login_protocol_scheme" />
        </intent-filter>
    </activity>
    
  2. Create Facebook CallBackManager variable

    var fbCallManager = CallbackManager.Factory.create()
    
  3. On Click of Login with Facebook button. Put your required permission in Array.

      LoginManager.getInstance()
        .logInWithReadPermissions(this, Arrays.asList("public_profile", "email", "user_friends"))
      LoginManager.getInstance().registerCallback(fbCallManager, object : FacebookCallback<LoginResult> {
        override fun onSuccess(result: LoginResult?) {
           //login success
        }
    
        override fun onCancel() {
            //login cancelled by user
    
        }
    
        override fun onError(error: FacebookException?) {
            //login error handle exception
        }
    
    })
    

    }

  4. Add callback result in onActivityResult method

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        fbCallManager.onActivityResult(requestCode, resultCode, data)
    }
    
  5. Provide the Development and Release Key Hashes for Your

    keytool -exportcert -alias androiddebugkey -keystore "C:\Users\USERNAME.android\debug.keystore" | "PATH_TO_OPENSSL_LIBRARY\bin\openssl" sha1 -binary | "PATH_TO_OPENSSL_LIBRARY\bin\openssl" base64

Download openssl from here

  1. Setup your Keyhash and Launcher activity in Facebook developer console.

References : Facebook

查看更多
登录 后发表回答