Facebook android sdk LoginButton使两次进入该网站

时间:2018-12-16 16:05:55

标签: android kotlin facebook-sdk-4.0

我使用Facebook android-sdk。当我尝试使用内置Web表单在没有Facebook本机应用程序的情况下登录时,Facebook尝试登录两次。 我found认为问题是Facebook LoginButton。因此,我想知道是否有某种方法可以保留FacebookLoginButton(而不是常规按钮),并且如果用户没有本机Facebook应用程序,则不要登录两次

MyFacebookActivity的代码

class MyFacebookActivity : AppCompatActivity() {

private var callbackManager: CallbackManager? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_my_facebook)
    AppEventsLogger.activateApp(this)

    // This button is from facebook sdk (com.facebook.login.widget.LoginButton)
    facebookSignInButton.setOnClickListener{
        callbackManager = CallbackManager.Factory.create()
        LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "default"))
        LoginManager.getInstance().registerCallback(callbackManager,
            object : FacebookCallback<LoginResult> {
                override fun onSuccess(result: LoginResult) {
                    Log.d("TAG", "Facebook token ${result.toString}")

                    val request = GraphRequest.newMeRequest(
                        result.accessToken
                    ) { `object`, response ->
                        Log.d("TAG", response.jsonObject.toString())
                    }

                    val parameters = Bundle()
                    parameters.putString("fields", "id,name")
                    request.parameters = parameters
                    request.executeAsync()
                }

                override fun onCancel() {
                    Log.d("FacebookTag", "Facebook onCancel")
                }

                override fun onError(error: FacebookException?) {
                    Log.d("FacebookTag", "Facebook onError")
                }

            })
    }

    facebookLogOutButton.setOnClickListener {
        LoginManager.getInstance().logOut()
    }
}

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

    callbackManager?.onActivityResult(requestCode, resultCode, data)
}

}

1 个答案:

答案 0 :(得分:1)

解决方案非常简单。对于Facebook LoginButton,您不需要onClickListener。它仅包含LoginManager的所有方法。 因此,正确的代码是:

callbackManager = CallbackManager.Factory.create()
    facebookSignInButton.setReadPermissions("public_profile")
    facebookSignInButton.registerCallback(callbackManager,
        object : FacebookCallback<LoginResult> {
            override fun onSuccess(result: LoginResult) {
                // stuff

                val request = GraphRequest.newMeRequest(
                    result.accessToken
                ) { `object`, response ->
                    // stuff
                }

                val parameters = Bundle()
                parameters.putString("fields", "id,name")
                request.parameters = parameters
                request.executeAsync()
            }

            override fun onCancel() {
                Log.d("FacebookTag", "Facebook onCancel")
            }

            override fun onError(error: FacebookException?) {
                Log.d("FacebookTag", "Facebook onError")
            }

        })