Flutter:使用 google_sign_in 时出现 PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null)

Flutter : PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null) when using google_sign_in

我已经按照 here 中的所有步骤进行操作,我还在 firebase 控制台中添加了 SHA1 密钥。

这是我的代码

void handleGoogleSignIn() async {
   String errorMessage = "success";

   GoogleSignInAccount _googleSignInAccount = await _googleSignIn.signIn();
   GoogleSignInAuthentication googleSignInAuthentication =
   await _googleSignInAccount.authentication;
   AuthCredential _credential = GoogleAuthProvider.getCredential(
       idToken: googleSignInAuthentication.idToken,
       accessToken: googleSignInAuthentication.accessToken);
   try {
     AuthResult _authResult = await _auth.signInWithCredential(_credential);
   } catch (error) {
     switch (error.code) {
       case "ERROR_INVALID_CREDENTIAL":
         errorMessage = "Invalid credentials";
         break;
       case "ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL":
         errorMessage = "This accouont exists withother credentials";
         break;
       case "ERROR_OPERATION_NOT_ALLOWED":
         errorMessage = "Signing in with Email and Password is not enabled.";
         break;
       default:
         errorMessage = "An undefined Error happened.";
     }
   }
   showErrorOrNavigate(errorMessage);
 }

我创建了一个调试。按照 these 个步骤

进行密钥库

异常:平台异常(sign_in_failed,com.google.android.gms.common.api.ApiException:10:,空) E/flutter (30467): #0 StandardMethodCodec.decodeEnvelope (包:flutter/src/services/message_codecs.dart:569:7) E/flutter (30467): #1 MethodChannel.invokeMethod (包:flutter/src/services/platform_channel.dart:321:33) E/flutter (30467): E/flutter (30467): #2 MethodChannel.invokeMapMethod (包:flutter/src/services/platform_channel.dart:349:48) E/flutter (30467): #3 MethodChannelGoogleSignIn.signIn (包:google_sign_in_platform_interface/src/method_channel_google_sign_in.dart:45:10)`

Firebase 控制台中添加的 SHA1 密钥证明:

我找不到我的错误。

今天我找到了处理它的方法。 阅读此讨论后: https://github.com/flutter/friendlychat-steps/issues/27

看看这个答案:

In Step 5, the signing key is the debug signing certificate (SHA-1) described in Configure Firebase for Android. You retrieved it from your development machine and copied it to the dialog in Firebase Console.

This key has to be from the machine you're developing on. If you have both a workstation and a laptop, make sure it matches the one where you're running friendlychat.

好吧,我正在寻找一种生成 SHA-1 的新方法并进行讨论:

试试吧!希望对你有帮助。

一个额外的答案可能会让陷入这种情况的人找到另一种方法。

如果您设置了:

  1. SHA-1
  2. 包 ID
  3. 下载 Google-Services.json / plist

但仍然面临这个问题,

请检查你的OAuth 2.0 Client IDs,如果你发现你的包没有OAuth 2.0 Client IDs,那么你必须创建它。

这里是实现解决方案的详细步骤:Another answer

我只添加了一个调试 SHA-1,所以它不能在许多 android 设备上使用检查这个答案:

它解决了我的问题,你应该在 firebase 上添加发布和调试 SHA-1