简体   繁体   English

google_sign_in Flutter 插件总是返回 null serverAccessCode

[英]google_sign_in Flutter plugin always returns null serverAccessCode

I have the current workflow for my authentication我有当前的身份验证工作流程

  1. User signs in via google OAuth2用户通过 google OAuth2 登录
  2. User is then given a server_auth_code which they send to my backend authentication然后给用户一个 server_auth_code,他们将其发送到我的后端身份验证
  3. The auth code is validated on the back end and users is sent a JWT验证码在后端进行验证,并向用户发送 JWT

I had this all working in raw Java with the Android SDK, but Flutter seemed a lot nicer.我用 Android SDK 在原始 Java 中完成了这一切,但 Flutter 似乎要好得多。 But now when using the google_sign_in plugin on android, I am unable to retrieve the serverAuthCore anymore, this plugin just wants to return null the entire time.但是现在在 android 上使用google_sign_in插件时,我无法再检索 serverAuthCore,这个插件只想一直返回 null。

I Am using the client ID that's specified for Android, however, I tested the WebApplication that's auto-generated by google too but that's the same issue (Null serverAutHCode)我正在使用为 Android 指定的客户端 ID,但是,我也测试了由 google 自动生成的 WebApplication,但这是同样的问题(Null serverAutHCode)

This is the code that I am currently using:这是我目前正在使用的代码:

/// Provides the `GoogleSignIn` class
import 'package:google_sign_in/google_sign_in.dart';

class GoogleLoginPage extends StatefulWidget {

  final String name = "Logging in with google.";

  late GoogleSignIn _googleSignIn;

  GoogleLoginPage() : super() {
    _googleSignIn = GoogleSignIn(
        scopes: ['https://www.googleapis.com/auth/userinfo.email',
        'https://www.googleapis.com/auth/userinfo.profile'],
        serverClientId: "XX-XXX.apps.googleusercontent.com"
    );
  }

  Future<void> fetchAuthToken() async {
    try {
      var response = await _googleSignIn.signIn();
      log(response?.serverAuthCode ?? "No server auth code");
      _googleSignIn.signOut();
    } catch (error) {
      print("ERR");
      print(error);
    }
  }

  @override
  State<GoogleLoginPage> createState() => GoogleLoginState();

}

The output of this code is: [log] No server auth code这段代码的输出是: [log] No server auth code

The question:问题:

Am I doing something wrong?难道我做错了什么? As mentioned this works 100% on my java project using the google play services SDK so I know it's nothing to do with my google console configurations.如前所述,这在我使用 google play services SDK 的 java 项目上 100% 有效,所以我知道这与我的 google 控制台配置无关。

Okay so I figured out the issues:好的,所以我发现了问题:

It appears that by default the google login plugin for flutter comes on an older version (If I remember correctly it was 20.0.5)似乎默认情况下,用于颤振的谷歌登录插件来自旧版本(如果我没记错的话它是 20.0.5)

I Simply changed the version to the latest version:我只是将版本更改为最新版本:

'com.google.android.gms:play-services-auth:20.2.0'

You can do this by editing the project's build.gradle (In IntelliJ you open build.gradle and click "Open for editing in the android studio" in the top right, from there you need to find the gradle file for google_sign_in, and change the import there, remember to click sync in the top right of android studio before you close out of it)您可以通过编辑项目的 build.gradle 来做到这一点(在 IntelliJ 中,您打开 build.gradle 并单击右上角的“在 android studio 中打开以进行编辑”,从那里您需要找到 google_sign_in 的 gradle 文件,然后更改在那里导入,记得在关闭之前点击android studio右上角的同步)

And I began to receive my serverAuthCode as normal, cheers!我开始正常接收我的 serverAuthCode,干杯!

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

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