繁体   English   中英

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

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

我已经尝试了这里提到的许多解决方案,但我仍然无法解决这个问题。 我收到:

“谷歌登录失败。com.google.android.gms.common.api.ApiException:10”

我已经在 Google Cloud Console 中验证了我的 OAuth 自动生成的“网络客户端密钥”与我在代码中使用的相同 但我遇到了同样的错误。 我还验证了 Google Cloud Console 和 Firebase 中的项目名称相同。

这是我的代码:

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

我正在关注官方文档中的教程。 任何帮助表示赞赏。

PS 我也在同一个项目中使用 Firestore 数据库。 我已经成功地将 google-services.json 保存在应用程序的文件夹中。

上传到播放商店后,播放商店生成自己的SHA1 ,我们必须在谷歌控制台的第二个密钥中替换它,并确保将SHA1添加到firebase项目中。

Go 到谷歌控制台>应用签名>复制 SH1 由控制台生成。 在 google 登录控制台中添加它,从 2nd key替换它。

在此处输入图像描述

看看这个,如果它帮助你: 谷歌登录失败 com.google.android.gms.common.api.ApiException:10:

暂无
暂无

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

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