Flutter通过Flutter idToken进行Google登录

时间:2019-12-26 14:15:47

标签: firebase flutter firebase-authentication google-signin

我正在尝试从客户端Flutter应用中获取idToken并发送回后端进行验证,问题是我要么没有获得idToken而是一个“ instanceOfGetIDToken”或类似的东西,否则它就会吐出来令牌与链接到的Firebase应用程序没有任何关系,我很困惑,因为用户出现在该应用程序的Firebase控制台中,但是在同意屏幕上提到了idNumber而不是名称,当我检查JWT时它有一些好的信息,但发行人和所有内容都不同?

final GoogleSignIn _googleSignIn = GoogleSignIn();
final FirebaseAuth _auth = FirebaseAuth.instance;
Future<FirebaseUser> _handleSignIn() async {
  final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
  final GoogleSignInAuthentication googleAuth = await googleUser.authentication;

  final AuthCredential credential = GoogleAuthProvider.getCredential(
    idToken: googleAuth.idToken,
  );

  final FirebaseUser user = (await _auth.signInWithCredential(credential)).user;
  print(credential);
  return user;
}

4 个答案:

答案 0 :(得分:0)

我认为您想要访问令牌

AuthCredential authCredential = GoogleAuthProvider.getCredential(
        idToken: googleAuth.idToken, accessToken: googleAuth.accessToken);

答案 1 :(得分:0)

如何使用呢?

Future<FirebaseUser> _handleSignIn() async {
  final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
  final GoogleSignInAuthentication googleAuth = await googleUser.authentication;

  final AuthCredential credential = GoogleAuthProvider.getCredential(
    idToken: googleAuth.idToken,
    accessToken: googleAuth.accessToken, 
  );

  final FirebaseUser user = (await _auth.signInWithCredential(credential)).user;

  print(user);
  return user;
}

答案 2 :(得分:0)

您似乎在Firebase项目中使用从Google登录过程中获得的ID令牌。我不确定Google登录令牌是否是Firebase项目的有效ID令牌。相反,您需要将Google凭据交换为Firebase凭据(就像您对signInWithCredential所做的那样),并使用以下命令从 that 获取ID令牌:

user.getIdToken()

答案 3 :(得分:0)

我弄清楚了,起初我不知道为什么令牌有一个奇怪的发行者和不同的签名模式,我可能正在获得Google API令牌或类似的东西。之后,我把它弄乱了,让它吐出了正确的令牌,但不是有效的令牌,结果日志切出了1000个字符。再次尝试而不复制,但直接使用验证idToken,它可以:D再次感谢!

  final GoogleSignIn _googleSignIn = GoogleSignIn();
  final FirebaseAuth _auth = FirebaseAuth.instance;
  Future<FirebaseUser> _handleSignIn() async {
  final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
  final GoogleSignInAuthentication googleAuth = await googleUser.authentication;

  final AuthCredential credential = GoogleAuthProvider.getCredential(
  idToken: googleAuth.idToken,
  accessToken: googleAuth.accessToken,
  );

  final FirebaseUser user = (await _auth.signInWithCredential(credential)).user;
  IdTokenResult mytoken = await user.getIdToken();
  String token = mytoken.token.toString();
  return user;
  }