Search code examples
androidandroid-fragmentskotlinfacebook-loginandroid-dialogfragment

Android Facebook login fails without an exception, just "Sorry something went wrong"


I'm trying to implement Facebook login into my app, and I'm facing a very peculiar issue, where the login process begins, fails with a very cryptic message and no exception is thrown on my app's side so I don't know what's going on.

I'm suspecting there is something to do with me using a DialogFragment for my login process, and the Facebook LoginButton is part of this DialogFragment instead of a normal fragment or activity. I add the login button in my XML:

 <com.facebook.login.widget.LoginButton
        android:id="@+id/loginFacebookButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/default_margin_double"
        android:layout_marginEnd="@dimen/default_margin_double"
        android:layout_marginBottom="@dimen/default_margin"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:background="@drawable/rounded_blue_bg"
        android:layout_marginTop="@dimen/default_margin"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:paddingLeft="@dimen/default_margin_double"
        app:layout_constraintBottom_toTopOf="@id/loginOrLayout" />

And I add the callbacks in code, in my onCreateDialog

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    val dialog = super.onCreateDialog(savedInstanceState)
    setStyle(STYLE_NORMAL, R.style.FullscreenDialog)

    val view = View.inflate(context, R.layout.fragment_login, null)

    //FB Login callback manager
    val callbackManager = CallbackManager.Factory.create()
    val fbLoginButton = view.loginFacebookButton
    fbLoginButton.fragment = this
    fbLoginButton.setPermissions(listOf("name", "email", "public_profile"))
    fbLoginButton.loginBehavior = LoginBehavior.NATIVE_WITH_FALLBACK
    fbLoginButton.registerCallback(callbackManager,
            object : FacebookCallback<LoginResult> {
                override fun onSuccess(loginResult: LoginResult) {
                    fbLoginSuccessful(loginResult)
                }

                override fun onCancel() {}

                override fun onError(exception: FacebookException) {
                    Log.w(TAG, "onError: Failed to login with FB.", exception)
                    loginFailed(exception.localizedMessage)
                }
            })

    return dialog
}

When I press the login button, I get a loading ProgressBar, then the Facebook app opens, I tap on my account, and then after some more loading, I get this screen:

Facebook Login Error

I've followed the documentation here but I cannot seem to get it to work. Any help is greatly appreciated. I even printed the key hash from within my code as mentioned here in the documnetation, and I have the correct hashes ion the developer console, but still get the errors. Thank you


Solution

  • It seems like I was missing the following in my fragment:

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        callbackManager.onActivityResult(requestCode, resultCode, data)
        super.onActivityResult(requestCode, resultCode, data)
    }
    

    However, It might have not been related, since the error was being thrown before the actual FB activity returned, so maybe it was an internal facebook error that has been fixed.

    Good luck to anyone facing similar issues, the support for the FB SDK is virtually non existent.