[英]com.google.android.gms.common.api.ApiException: 10:
I want do Google SignIn using below gradle.我想使用以下 gradle 进行 Google SignIn。
implementation 'com.google.android.gms:play-services-auth:15.0.0'
Below code for initialisation :下面的初始化代码:
private fun initGooglePlus() {
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.server_client_id))
.requestEmail()
.build()
mGoogleSignInClient = GoogleSignIn.getClient(this.activity!!, gso)
}
I am calling below code in my button click我在我的按钮点击中调用下面的代码
private fun googlePlusLogin() {
val signInIntent = mGoogleSignInClient!!.signInIntent
startActivityForResult(signInIntent, SIGN_IN_CODE)
}
OnActivityForResult活动结果
override
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == SIGN_IN_CODE) {
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
handleSignInResult(task)
}
}
I am getting below exception in onActivityForResult :我在 onActivityForResult 中遇到以下异常:
com.google.android.gms.common.api.ApiException: 10:
at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(Unknown Source)
at com.google.android.gms.auth.api.signin.GoogleSignIn.getSignedInAccountFromIntent(Unknown Source)
I have tried both Android Auth key and Web Auth key.我已经尝试了 Android Auth 密钥和 Web Auth 密钥。 I am always getting above exception.
我总是超越例外。
Please help me out.请帮帮我。
This exception code means configuration problems with your app.此异常代码表示您的应用程序存在配置问题。 I was having the same problem, and it was solved, for me, this way (in my case, I wanted firebase authentication with google sign in mechanism):
我遇到了同样的问题,对我来说,它以这种方式解决了(就我而言,我想要使用 Google 登录机制进行 Firebase 身份验证):
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
... probably you need Android auth ... 可能你需要 Android 身份验证
I will copy the important part of my code (it's working and written in Java), but I think, by your exception message, that there is nothing wrong with your code.我将复制我的代码的重要部分(它是用 Java 编写的),但我认为,根据您的异常消息,您的代码没有任何问题。
void onCreate(...) {
firebaseAuth = FirebaseAuth.getInstance();
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.web_server_client_id))
.requestEmail()
.build();
FirebaseUser currentUser = firebaseAuth.getCurrentUser();
firebaseAuthUpdateUI(currentUser);
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
findViewById(getLoginButtonId()).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signIn();
}
});
}
protected void signIn(){
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
// Google Sign In failed, update UI appropriately
// ...
firebaseAuthUpdateUI(null);
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
//Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
//Log.d(TAG, "signInWithCredential:success");
user = firebaseAuth.getCurrentUser();
firebaseAuthUpdateUI(user);
} else {
// If sign in fails, display a message to the user.
//Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(SignInActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
user = null;
//firebaseAuthUpdateUI(null);
}
// ...
}
});
}
... ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.