我想使用下面的gradle做Google SignIn。
implementation 'com.google.android.gms:play-services-auth:15.0.0'
下面的初始化代码:
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)
}
我在按下按钮时调用下面的代码
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)
}
}
我在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)
我已经尝试过Android Auth密钥和Web Auth密钥。我总是超越异常。
请帮帮我。
答案 0 :(得分:0)
此异常代码表示configuration problems与您的应用。我遇到了同样的问题,对我来说,这种方式已经解决了(在我的情况下,我希望使用google登录机制进行firebase身份验证):
- 我使用谷歌自动生成的网络客户端ID而不是我创建的(我甚至没有要求谷歌生成它 - 它真的是自动的)
- 我在我的android项目中更新了我的JSON firebase文件(可能没有必要,但我的选项已经用完了)
一个可能有用的观察:
- 如果你有这样的一条线......
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
...可能你需要Android认证
我将复制我的代码的重要部分(它的工作和用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); } // ... } }); } ...