com.google.android.gms.common.api.ApiException:10: [英] com.google.android.gms.common.api.ApiException: 10:

查看:188
本文介绍了com.google.android.gms.common.api.ApiException:10:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使用下面的gradle进行Google登录.

I want do Google SignIn using below gradle.

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

我在按钮单击中调用以下代码

I am calling below code in my button click

private fun googlePlusLogin() {
        val signInIntent = mGoogleSignInClient!!.signInIntent
        startActivityForResult(signInIntent, SIGN_IN_CODE)
    }

OnActivityForResult

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中遇到以下异常:

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)

我已经尝试了Android身份验证密钥和Web身份验证密钥.我总是超越例外.

I have tried both Android Auth key and Web Auth key. I am always getting above exception.

请帮帮我.

推荐答案

此异常代码表示

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自动生成的网络客户端ID,而不是我自己创建的ID(我什至没有要求google生成它-确实是自动生成的)
  • 我在我的android项目中更新了我的JSON firebase文件(这可能不是必需的,但是我用完了所有选项)
    可能有用的观察结果:
  • 如果您有这样的一行...
  • 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);

...可能您需要Android身份验证

... probably you need Android auth

我将复制代码的重要部分(它正在工作并用Java编写),但是我认为,根据您的异常消息,您的代码没有任何问题.

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

                    // ...
                }
            });
}

...

这篇关于com.google.android.gms.common.api.ApiException:10:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆