com.google.android.gms.common.api.ApiException:10:

问题描述 投票:0回答:1

我想使用下面的 gradle 进行 Google 登录。

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 密钥。我总是遇到异常。

请帮帮我。

android google-play-services google-plus
1个回答
0
投票

此异常代码意味着您的应用程序存在配置问题。我遇到了同样的问题,对我来说,它已经解决了(就我而言,我想要使用谷歌登录机制进行 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);
                    }

                    // ...
                }
            });
}

...

© www.soinside.com 2019 - 2024. All rights reserved.