简体   繁体   English

在 Android(仅在 iOS 上)上无法使用 Google 登录 - Flutter

[英]Sign in with Google is not working on Android (only on iOS) - Flutter

It seems that the Sign in with Google plugin is working on iOS but not on Android.似乎使用 Google 登录插件在 iOS 上运行,但在 Android 上运行。 I'm currently using the latest version (google_sign_in 5.0.2).我目前使用的是最新版本(google_sign_in 5.0.2)。 I have already utilized any possible configurations on Firebase console by filling up the SHA fingerprints and the its credentials.通过填写 SHA 指纹及其凭据,我已经在 Firebase 控制台上使用了任何可能的配置。 I have also placed the google-services.json and GoogleService-Info.plist files on the Flutter project.我还在 Flutter 项目中放置了 google-services.json 和 GoogleService-Info.plist 文件。

To check the validity of the token, I used this ( https://www.googleapis.com/oauth2/v1/tokeninfo?id_token= ) and one thing I have noticed is that whenever a token is generated from an Android device, it would show different values on issued_to and audience .为了检查令牌的有效性,我使用了这个( https://www.googleapis.com/oauth2/v1/tokeninfo?id_token= ),我注意到的一件事是,每当从 Android 设备生成令牌时,它会在issue_toAudience上显示不同的值。 I checked on the Firebase project's credentials and the OAuth client id being used on the audience is from a Web Application and not from an Android device (I'm only developing for iOS and Android devices). I checked on the Firebase project's credentials and the OAuth client id being used on the audience is from a Web Application and not from an Android device (I'm only developing for iOS and Android devices). Both tokens generated from iOS and Android were verified tokens from JWT.从 iOS 和 Android 生成的两个令牌都是来自 JWT 的已验证令牌。

With that, whenever this token is being validated by the backend, with this guide ( https://github.com/google/google-id-token ), it would return a token client-id mismatch.这样,每当后端验证此令牌时,使用本指南( https://github.com/google/google-id-token ),它将返回令牌客户端 ID 不匹配。

I'm not really sure what's the cause since I have already recreated the Firebase project like three times and I would still get the same error.我不太确定是什么原因,因为我已经重新创建了 Firebase 项目 3 次,但我仍然会遇到同样的错误。 Any cause?有什么原因吗?

Additional information:附加信息:

Verified: https://www.googleapis.com/oauth2/v1/tokeninfo?id_token=已验证: https://www.googleapis.com/oauth2/v1/tokeninfo?id_token=

iOS: iOS:

{
  "issued_to": "60204403274-jvgooi7...",
  "audience": "60204403274-jvgooi7...",
  "user_id": "1003...",
  "expires_in": 3583,
  "email": "dean...",
  "email_verified": true,
  "issuer": "https://accounts.google.com",
  "issued_at": 1619....,
  "nonce": "IF4Y3..."
}

Android: Android:

{
  "issued_to": "60204403274-uqoa6ss...",
  "audience": "60204403274-7uh25jv2...",
  "user_id": "10036...",
  "expires_in": 1592,
  "email": "dean...",
  "email_verified": true,
  "issuer": "https://accounts.google.com",
  "issued_at": 1619...
}

Error returned:返回错误:

{status: Cannot validate: Token client-id mismatch}

Firebase console: https://i.stack.imgur.com/THYx5.png Firebase 控制台: https://i.stack.imgur.com/THYx5.png

can you check if in manifest you have:你能检查一下清单中是否有:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Check this page 检查此页面

As per your logs nonce is missing for android.根据您的日志,android 缺少随机数。 so add nonce and test again.所以添加随机数并再次测试。

  /*  Create a nonce for this request.
   Here we append the string to a number of random bytes to ensure it larger
    than the minimum 16 bytes required.
    Read out this value and verify it against the original request to ensure the
    response is correct and genuine.
    NOTE: A nonce must only be used once and a different nonce should be used for each request.
    As a more secure option, you can obtain a nonce from your own server using a secure
    connection. Here in this sample, we generate a String and append random bytes, which is not
    very secure. */

Follow the tips on the Security Tips page for more information: / https://developer.android.com/training/articles/security-tips.html#Crypto按照安全提示页面上的提示了解更多信息:/ https://developer.android.com/training/articles/security-tips.html#Crypto

 // TODO(developer): Change the nonce generation to include your own, used once value,
        // ideally from your remote server.

        String nonceData = "Sample: " + System.currentTimeMillis();
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        Random mRandom = new SecureRandom();

        byte[] bytes = new byte[24];
        mRandom.nextBytes(bytes);
        try {
            byteStream.write(bytes);
            byteStream.write(nonceData.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }

        byte[] nonce = byteStream.toByteArray();

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

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