我正在尝试将Facebook登录功能集成到我的应用程序中,但我遇到了一个非常奇怪的问题:登录过程开始后,出现了一个非常神秘的错误消息,并且我的应用程序没有抛出任何异常,所以我不知道发生了什么。
我怀疑这与我在登录过程中使用DialogFragment有关,而Facebook的LoginButton是该DialogFragment的一部分,而不是普通的fragment或activity。我在XML中添加了登录按钮:
当我按下登录按钮时,会出现一个加载进度条,然后打开Facebook应用程序,我点击我的帐户,然后经过一些加载后,会出现这个屏幕: 我已经按照这里的文档进行操作,但似乎无法使其工作。非常感谢任何帮助。我甚至像文档中提到的那样在我的代码中打印了密钥哈希,并且在开发者控制台中也有正确的哈希值,但仍然出现错误。谢谢。
我怀疑这与我在登录过程中使用DialogFragment有关,而Facebook的LoginButton是该DialogFragment的一部分,而不是普通的fragment或activity。我在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" />
我会在我的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
}
当我按下登录按钮时,会出现一个加载进度条,然后打开Facebook应用程序,我点击我的帐户,然后经过一些加载后,会出现这个屏幕: 我已经按照这里的文档进行操作,但似乎无法使其工作。非常感谢任何帮助。我甚至像文档中提到的那样在我的代码中打印了密钥哈希,并且在开发者控制台中也有正确的哈希值,但仍然出现错误。谢谢。