[英]Custom button for facebook login using Kotlin but failed
I would like to apply facebook login module by clicking the custom button and acquire access token from facebook, then pass the access token from Facebook to firebase to Login. 我想通过单击自定义按钮来应用Facebook登录模块,并从Facebook获取访问令牌,然后将访问令牌从Facebook传递到Firebase并登录。
When it comes the execution, there has no response from register call back. 执行时,没有来自寄存器回调的响应。
I have setup the Facebook Developer App called : JacksonApplication and the application written in Kotlin as below: 我已经安装了名为:JacksonApplication的Facebook Developer App和用Kotlin编写的应用程序,如下所示:
Would you please tell me what are other ways I have to implement ? 您能告诉我我还有哪些其他实现方式?
class JacksonApplication : Application() {
override fun onCreate() {
super.onCreate()
AppEventsLogger.activateApp(this);
JodaTimeAndroid.init(this);
}
}
My function : 我的职能:
callbackManager = CallbackManager.Factory.create()
LoginManager.getInstance()
LoginManager.getInstance().registerCallback(callbackManager ,
object : FacebookCallback<LoginResult> {
override fun onSuccess(loginResult: LoginResult) {
println("facebook loginResult : $loginResult")
handleFacebookAccessToken(loginResult.accessToken);
// App code
}
override fun onCancel() {
// App code
}
override fun onError(exception: FacebookException) {
println("loginResult : ${exception.localizedMessage}")
// App code
}
})
fun handleFacebookAccessToken( token : AccessToken) {
Log.d(TAG, "handleFacebookAccessToken:" + token);
val credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth!!.signInWithCredential(credential)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// Sign in success
val firebaseUser = mAuth!!.currentUser
updateUI(firebaseUser)
} else {
// Sign in fails
val currentUser = mAuth!!.currentUser!!
user.register(currentUser.uid)
}
}
}
My actions : 我的动作:
private fun loginSocial( type : String ) {
if (type == "Facebook") {
println("execute social reg by facebook")
callbackManager = CallbackManager.Factory.create()
LoginManager.getInstance()
LoginManager.getInstance().registerCallback(callbackManager ,
object : FacebookCallback<LoginResult> {
override fun onSuccess(loginResult: LoginResult) {
println("facebook loginResult : $loginResult")
handleFacebookAccessToken(loginResult.accessToken);
// App code
}
override fun onCancel() {
// App code
}
override fun onError(exception: FacebookException) {
println("loginResult : ${exception.localizedMessage}")
// App code
}
})
}else if(type == "Google") {
println("execute social reg by google")
val intent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient)
startActivityForResult(intent, REQUEST_CODE_GOOGLE_SIGN_IN)
mGoogleApiClient!!.connect()
}
}
onActivity Result : onActivity结果:
override fun onActivityResult(requestCode: Int , resultCode: Int , data: Intent?) {
super.onActivityResult(requestCode , resultCode , data)
callbackManager?.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE_GOOGLE_SIGN_IN) {
progressDialog = ProgressDialog(this )
progressDialog!!.setMessage(this.resources.getString(R.string.loading))
progressDialog!!.setCancelable(false)
progressDialog!!.show()
val result = Auth.GoogleSignInApi.getSignInResultFromIntent(data)
if (result.isSuccess) {
// successful -> authenticate with Firebase
val account = result.signInAccount
firebaseAuthWithGoogle(account!!)
} else {
// failed -> update UI
updateUI(null)
Toast.makeText(applicationContext, "SignIn: failed!",
Toast.LENGTH_SHORT).show()
}
}else {
progressDialog = ProgressDialog(this )
progressDialog!!.setMessage(this.resources.getString(R.string.loading))
progressDialog!!.setCancelable(false)
progressDialog!!.show()
}
}
Add these lines in onActivityResult() 将这些行添加到onActivityResult()中
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == YOUR_GOOGLE_REQUEST_CODE) {
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
try {
// Google Sign In was successful, authenticate with Firebase
val account = task.getResult(ApiException::class.java)
firebaseAuthWithGoogle(account!!)
} catch (e: ApiException) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e)
// ...
}
} else {
// Pass the activity result back to the Facebook SDK
callbackManager.onActivityResult(requestCode, resultCode, data)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.