简体   繁体   English

有没有人设法从谷歌登录 (Flutter) 获取 id 令牌

[英]Did anyone manage to get the id token from google sign in (Flutter)

I am trying to connect my users with my back end server, i used the example from the official google sign in plugin for flutter: https://pub.dartlang.org/packages/google_sign_in我正在尝试将我的用户与我的后端服务器连接起来,我使用了 flutter 的官方谷歌登录插件中的示例: https://pub.dartlang.org/packages/google_sign_in

the sign process goes fine and i get the username and email ect.. but i need the id Token to authenticate the user with my server.签名过程很顺利,我得到了用户名和 email 等。但我需要 id 令牌来通过我的服务器对用户进行身份验证。

Ps: Not using firebase, only google sign in. ps:没有使用firebase,只能google登录。

Can anyone guide me how to get the id Token?谁能指导我如何获得 id 令牌?

You can try using this你可以尝试使用这个

 _googleSignIn.signIn().then((result){
          result.authentication.then((googleKey){
              print(googleKey.accessToken);
              print(googleKey.idToken);
              print(_googleSignIn.currentUser.displayName);
          }).catchError((err){
            print('inner error');
          });
      }).catchError((err){
          print('error occured');
      });

or try like this if id token was null, it worked for me.或者如果 id 令牌为空,则尝试这样,它对我有用。

As the docs point out you need oauth2 client id of your backend to request idToken or serverAuthCode.正如文档指出的那样,您需要后端的 oauth2 客户端 ID 来请求 idToken 或 serverAuthCode。

from firebase google sigin in authentication copy the Web SDK configuration add paste in the following to res/values/strings.xml, That should work在身份验证中从 firebase google sigin 复制 Web SDK 配置并将以下内容粘贴到 res/values/strings.xml,这应该可以

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="default_web_client_id">{Web app Client id goes here}</string>
</resources>

You can get access token and id token more simple like this:您可以像这样更简单地获取访问令牌和 id 令牌:

final result = await _googleSignIn.signIn();
final ggAuth = await result.authentication;
print(ggAuth.idToken);
print(ggAuth.accessToken);

Or you also can add it to try-catch to handle an error.或者您也可以将其添加到 try-catch 以处理错误。

try {
   final result = await _googleSignIn.signIn();
   final ggAuth = await result.authentication;
   print(ggAuth.idToken);
   print(ggAuth.accessToken);
} catch (error) {
  print(error);
}

To retrieve the Is idToken worked for me:检索 Is idToken 为我工作:

1. The google-services.json file must be placed in /android/app/ 1google-services.json文件必须放在/android/app/

2. you need to add to your /android/app/build.gradle 2.你需要添加到你的/android/app/build.gradle

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

3. and to /android/build.gradle 3.到/android/build.gradle

classpath 'com.google.gms:google-services:4.3.4'

And that's it.就是这样。 GoogleSignIn will return a real idToken instead of null. GoogleSignIn 将返回一个真实的 idToken 而不是 null。

font: https://github.com/flutter/flutter/issues/12140#issuecomment-348720774字体: https ://github.com/flutter/flutter/issues/12140#issuecomment-348720774

I was struggling with this issue for about a month.我在这个问题上挣扎了大约一个月。 Turns out I was getting the same access token, even when the user tried restarting the app.事实证明,即使用户尝试重新启动应用程序,我也获得了相同的访问令牌。 This was painful because my app dealt with scopes and in case a user misses to check one or more scopes in his first sign in, the app wouldn't work at all, even if he/she signs in again and gives the permissions.这很痛苦,因为我的应用程序处理范围,如果用户在第一次登录时错过检查一个或多个范围,应用程序根本无法工作,即使他/她再次登录并授予权限。

Workaround which worked for me: I called the googleSignIn.currentUser.clearAuthCache() method followed by googleSignIn.signInSilently() .对我有用的解决方法:我调用了googleSignIn.currentUser.clearAuthCache()方法,然后调用了 googleSignIn.signInSilently googleSignIn.signInSilently() This returns a GoogleSignInAccount which can be used for further authentication.这将返回一个可用于进一步身份验证的 GoogleSignInAccount。 My guess is that the clearAuthCache() method clears the token cache and hence a new token is created.我的猜测是 clearAuthCache() 方法清除了令牌缓存,因此创建了一个新令牌。 This should get you a new access token and let your app make valid calls.这应该会为您提供一个新的访问令牌并让您的应用程序进行有效调用。

I sincerely request Google developers to solve this issue.我真诚地请求谷歌开发者解决这个问题。 For now, this workaround is the only thing that worked.目前,这种解决方法是唯一有效的方法。

One more clean way to achieve this:实现此目的的另一种更简洁的方法:

late Map<String, dynamic> userObject = {};
var res = await _googleSignIn.signIn();
var googleKey = await res!.authentication;
userObject.addAll({
  'accessToken': googleKey.accessToken,
  'idToken': googleKey.idToken,
  'displayName': res.displayName ?? '',
  'email': res.email,
  'id': res.id,
  'avatarUrl': res.photoUrl ?? '',
  'serverAuthCode': res.serverAuthCode ?? '',
});

The issue may be related to not using firebase.该问题可能与不使用 firebase 有关。 There is a google-services.json file which is given to you when you register your app in firebase, and you can use that with google_sign_in (this is the default way shown in the documentation).当您在 firebase 中注册您的应用程序时,会提供一个 google-services.json 文件,您可以将其与 google_sign_in 一起使用(这是文档中显示的默认方式)。

I was getting null for the token value when trying to implement this without the google-services.json, but successfully signing into google.尝试在没有 google-services.json 的情况下实现此功能时,我的令牌值为空,但成功登录谷歌。

If you don't want to use firebase, you have to jump through a couple hoops.如果您不想使用 firebase,则必须跳过几圈。

  • In google cloud console, register your app.在谷歌云控制台中,注册您的应用程序。
  • Then make sure you are 'in' your app that you just created in the top drop down menu.然后确保您“在”您刚刚在顶部下拉菜单中创建的应用程序。
  • in "apis and services" in the sidebar menu, go through the create Oauth consent screen menu, I don't remember having to fill out many fields, so leave them blank if you don't know what to put in.在侧边栏菜单的“api 和服务”中,通过创建 Oauth 同意屏幕菜单,我不记得必须填写很多字段,所以如果您不知道要输入什么,请将它们留空。
  • then go to the "credentials" menu in the sidebar, and click "Create New Credentials", and select OAuth2 client ID.然后转到侧边栏中的“凭据”菜单,单击“创建新凭据”,然后选择 OAuth2 客户端 ID。 Make a web client, even though you're trying to use it with an android/ios app.制作一个 Web 客户端,即使您尝试将它与 android/ios 应用程序一起使用。
  • Make a file android/app/src/main/res/values/strings.xml制作一个文件android/app/src/main/res/values/strings.xml
  • using the web client we just made, insert <?xml version="1.0" encoding="utf-8"?> <resources> <string name="default_web_client_id">YOUR WEB CLIENT ID</string> </resources> into the strings.xml file.使用我们刚刚制作的 Web 客户端,将<?xml version="1.0" encoding="utf-8"?> <resources> <string name="default_web_client_id">YOUR WEB CLIENT ID</string> </resources>插入strings.xml 文件。
  • [edit] make one more client in the google console for android, and put in your local machine's sha1 key. [编辑] 在 android 的 google 控制台中再创建一个客户端,并放入本地机器的 sha1 密钥。 This step is done for you automatically if you're using firebase.如果您使用的是 firebase,此步骤会自动为您完成。 In this case, you have to create both the web client and one for your android device.在这种情况下,您必须为您的 Android 设备创建一个 Web 客户端和一个。 In production, you'd be using a specific client for your production app.在生产中,您将为您的生产应用程序使用特定的客户端。

That should do it I believe, might have missed a step.我相信应该这样做,可能错过了一步。

I also wanted to verify on my backend that the incoming idtoken was valid, so I had to also make a service account (in the apis and services -> credentials page) and use that in my go server.我还想在后端验证传入的 idtoken 是否有效,因此我还必须创建一个服务帐户(在 apis 和服务 -> 凭据页面中)并在我的 go 服务器中使用它。

I'm still struggling to get this to work with ios, but the android side works great.我仍然在努力让它与 ios 一起使用,但 android 端效果很好。

Try this:尝试这个:

When you create your GoogleSignIn object:当您创建 GoogleSignIn object 时:

GoogleSignIn( clientId: "YOUR CLIENT ID" ) GoogleSignIn(clientId: "你的客户 ID")

i hope it helps;)我希望它有帮助;)

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

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