Flutter 使用 google_sign_in 包登录 google,返回的 idtoken 对于 android 为空

问题描述 投票:0回答:2

我有一个适用于 ios/android 的 flutter 移动应用程序。我决定使用 google_sign_in 包为 android 和 ios 添加使用 google 的社交登录。 我在 google 控制台上分别为 android 和 ios 创建了两个客户端 id。

在 flutter 中,我尝试使用以下代码获取 idtoken:

GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
GoogleSignInAuthentication googleAuth = await googleUser.authentication;
String? googleToken = googleAuth.idToken;

对于 ios,一切正常,我能够获取 idtoken 并将其发送到我的后端服务器。然而,对于 Android,我可以看到使用 google 登录的弹出窗口,选择 google 帐户,然后我得到的 idtoken 为 null。

oauth2 客户端 ID 设置为 Android 应用程序,并且 sha-1 证书已根据我的发布签名密钥正确注册。

值得一提的是,我没有使用 Firebase,也不想为此使用 Firebase。我想检索发送到后端进行验证的 idtoken,并为我的应用程序生成一个 jwt,我将其返回到 flutter 以用于进一步的调用。

有什么想法为什么这个 idtoken 对于 Android 来说为空而适用于 ios 吗? 谢谢!

android ios flutter oauth google-oauth
2个回答
0
投票
this solution woks for me
https://firebase.flutter.dev/docs/installation/android#installing-your-firebase-configuration-file

adding com.google.gms:google-services as a dependency inside of the android/build.gradle

  dependencies {
    // ... other dependencies
    classpath 'com.google.gms:google-services:4.3.8'
  }
}

adding apply plugin: 'com.google.gms.google-services' in /android/app/build.gradle

apply plugin: 'com.google.gms.google-services'

0
投票

如果您在使用 Google 登录时在 Android 上获得空 ID 令牌,一个常见问题是 Google 开发者控制台中的 OAuth 2.0 客户端 ID 未针对您的 Android 应用程序正确配置。

确保您已将 Android 应用签名密钥的 SHA-1 证书指纹与 Google 开发者控制台中的 OAuth 2.0 客户端 ID 正确关联。

验证您是否在 OAuth 2.0 客户端 ID 配置中为 Android 应用程序指定了正确的包名称。

确保您的 Android 应用程序在 AndroidManifest.xml 文件中具有必要的权限,包括 INTERNET 和 ACCESS_NETWORK_STATE。

在 Flutter 应用程序中仔细检查 Google Sign-In 的代码实现,以从 GoogleSignInAccount 对象检索 ID 令牌。

© www.soinside.com 2019 - 2024. All rights reserved.