authentication - 在 Flutter 中使用 Google Sign-In 获取用户的生日/性别

标签 authentication flutter dart firebase-authentication google-signin

我想使用 Firebase Auth 和 Google Sign-In 获取用户的生日和性别。不幸的是,登录发生后,我只得到用户的电子邮件、显示名称、照片网址和电话号码。我看到我可以向 GoogleSignIn 对象添加范围,我这样做 - https://www.googleapis.com/auth/user.birthday.read , https://www.googleapis.com/auth/userinfo.profile ,但是,登录后我仍然看不到任何其他数据。知道如何得到这个结果吗?因为当这些范围被添加到对象时,它会在登录之前询问我是否接受提供这些数据,所以我想应该有一种方法来获取和显示它们。

这是我的登录代码:

  final GoogleSignIn _googleSignIn = GoogleSignIn(scopes: ["email", "https://www.googleapis.com/auth/user.birthday.read", "https://www.googleapis.com/auth/userinfo.profile"]);
  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(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );

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

最佳答案

我希望这对面临同样问题的其他人有用:)

 GoogleSignIn googleSignIn = GoogleSignIn(scopes: ['email',"https://www.googleapis.com/auth/userinfo.profile"]);
  
  @override
  Future<User> login() async {
    await googleSignIn.signIn();
    User user = new User(
      email: googleSignIn.currentUser.email,
      name: googleSignIn.currentUser.displayName,
      profilePicURL: googleSignIn.currentUser.photoUrl,
      gender: await getGender()
    );
     
    return user;
  }

  Future<String> getGender() async {
    final headers = await googleSignIn.currentUser.authHeaders;
    final r = await http.get("https://people.googleapis.com/v1/people/me?personFields=genders&key=",
      headers: {
        "Authorization": headers["Authorization"]
      }
    );
    final response = JSON.jsonDecode(r.body);
    return response["genders"][0]["formattedValue"];
  }

关于authentication - 在 Flutter 中使用 Google Sign-In 获取用户的生日/性别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59844578/

相关文章:

flutter - 在Flutter中,我想通过按按钮获取当前日期并在Textfield中显示,该怎么办?

Flutter 自动完成功能在 VSCode 中不适用于包

flutter - 如何在 Flutter 中加密 SQLite 数据库?

json - Flutter:如何显示包含对象列表的已解析json对象中的数据

api - REST API 上的 CakePHP 身份验证

flutter - Flutter-仅当TextField失去焦点时才验证TextFormField

flutter - 有没有办法杀死计算函数创建的任务?

rest - 生成访问 token Instagram API,无需登录?

authentication - 用户通过电子邮件和密码身份验证登录到Firebase后,是否可以使用Google Auth链接用户Youtube视频?

authentication - Wildfly 8 基本身份验证