简体   繁体   English

无法在 Android 应用程序中使用 Google 登录

[英]Unable to sign-in using Google in Android App

I've tried many solutions mentioned here but I'm still unable to fix this.我已经尝试了这里提到的许多解决方案,但我仍然无法解决这个问题。 I'm receiving:我收到:

"Google sign in failed. com.google.android.gms.common.api.ApiException: 10" “谷歌登录失败。com.google.android.gms.common.api.ApiException:10”

I've verified my OAuth auto-generated 'web client key' in Google Cloud Console is same as what I'm using in my code.我已经在 Google Cloud Console 中验证了我的 OAuth 自动生成的“网络客户端密钥”与我在代码中使用的相同 But I'm getting the same error.但我遇到了同样的错误。 I've also verified that project name is same in Google Cloud Console and Firebase.我还验证了 Google Cloud Console 和 Firebase 中的项目名称相同。

Here's my code:这是我的代码:

public class LoginActivity extends Activity {

  private GoogleSignInClient mGoogleSignInClient;
  private FirebaseAuth mAuth = FirebaseAuth.getInstance();
  private static final int RC_SIGN_IN = 1;
  static LoginActivity loginActivity;
  private FirebaseUser currentUser;

  public static LoginActivity getInstance(){
    return loginActivity;
  }
  public FirebaseUser getCurrentUser() {
    return currentUser;
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    GoogleSignInOptions gso = new 
    GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();

    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
    mAuth = FirebaseAuth.getInstance();

    View signInButton = findViewById(R.id.sign_in_button);
    signInButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent signInIntent = mGoogleSignInClient.getSignInIntent();
            startActivityForResult(signInIntent, RC_SIGN_IN);
        }
    });
 }

 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        //Here resultCode is 0 & requestCode is 1.  
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try {
            // This line of code is throwing exception.
            GoogleSignInAccount account = task.getResult(ApiException.class);
            Log.w(TAG, task.getException());
            Log.d(TAG, "firebaseAuthWithGoogle:" + account.getId());
            firebaseAuthWithGoogle(account.getIdToken());
        } catch (ApiException e) {
            // Google Sign In failed, update UI appropriately
            Log.w(TAG, "Google sign in failed", e);
        }
    }
}

I'm following this tutorial from official docs.我正在关注官方文档中的教程。 Any help is appreciated.任何帮助表示赞赏。

PS I'm also using Firestore database in the same project. PS 我也在同一个项目中使用 Firestore 数据库。 And I've successfully saved the google-services.json in the app's folder.我已经成功地将 google-services.json 保存在应用程序的文件夹中。

After Uploading to play store , Play store generate its own SHA1 which we have to replace in second key in google console and Make sure you add your SHA1 to the firebase project.上传到播放商店后,播放商店生成自己的SHA1 ,我们必须在谷歌控制台的第二个密钥中替换它,并确保将SHA1添加到firebase项目中。

Go to google console>app signing> copy SH1 generate by console. Go 到谷歌控制台>应用签名>复制 SH1 由控制台生成。 Add that in google sign in console replace it from 2nd key .在 google 登录控制台中添加它,从 2nd key替换它。

在此处输入图像描述

look at this if it help you: Google sign in failed com.google.android.gms.common.api.ApiException: 10:看看这个,如果它帮助你: 谷歌登录失败 com.google.android.gms.common.api.ApiException:10:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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