简体   繁体   中英

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

I want do Google SignIn using below gradle.

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 :

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. 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):

  • I used a google automatically generated web client id instead of the one I had created (I didn't even ask google to generate it - it was really automatic)
  • I updated my JSON firebase file in my android project (it's probably not necessary but I was running out of options)
    An observation that may be helpful:
  • If you have a line like this...
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        handleSignInResult(task);

... probably you need Android auth

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.

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);
                    }

                    // ...
                }
            });
}

...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM