AccessToken.getCurrentAccessToken()始终在Kotlin中返回null

时间:2017-10-05 19:15:08

标签: android facebook kotlin

我在kotlin中使用Facebook Sdk。当我尝试使用FB共享侦听器AccessToken.getCurrentAccessToken()时返回null。我试过了FacebookSdk.sdkInitialize(this),但已经弃用了。我该如何解决这个问题

 fun SetFacebookShareListener(sd: ShareDialog) {

            sd.registerCallback(mCallbackManagerFacebook, object : FacebookCallback<Sharer.Result> {
                override fun onSuccess(result: Sharer.Result) {
                    // Toast.makeText(DetailsFragment.this.getActivity(), "You shared this post", Toast.LENGTH_SHORT).show();
                    val builder1 = android.app.AlertDialog.Builder(this@MainActivity)
                    val activeSession = AccessToken.getCurrentAccessToken()
                    if (activeSession == null) {
                        builder1.setMessage("Do you want us to Connect to your account with facebook?")
                    } else if (!activeSession.permissions.contains("publish_actions")) {
                        builder1.setMessage("Do you want us to automatically post your reward details at your facebook timeline?")
                    }

                    builder1.setPositiveButton("Yes") { dialog, which ->
                        // TODO Auto-generated method stub

                        this@MainActivity.ConnectWithFacebook()
                    }

                    builder1.setNegativeButton("No") { dialog, which ->
                        // TODO Auto-generated method stub
                        //  Toast.makeText(getApplicationContext(), "test1", Toast.LENGTH_LONG).show();
                    }

                    val alert = builder1.create()
                    alert.show()
                }

                override fun onCancel() {
                    Toast.makeText(applicationContext, "test2", Toast.LENGTH_LONG).show()
                }

                override fun onError(e: FacebookException) {
                    // Toast.makeText(getApplicationContext(), "test3", Toast.LENGTH_LONG).show();
                    e.printStackTrace()
                }
            })
        }

和Facebook连接

 fun ConnectWithFacebook() {


    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "email", "user_birthday"))

}

1 个答案:

答案 0 :(得分:1)

LoginManager返回的访问令牌将保存在共享首选项中,因此下次打开应用程序时,AccessToken.getCurrentAccessToken()应具有相同的访问令牌。您可以查看随SDK提供的示例,看看它们的工作情况。