Get user's birthday/gender using Google Sign-In in Flutter

≡放荡痞女 提交于 2021-01-21 04:58:31

问题


I want to get user's birthday and gender using Firebase Auth and Google Sign-In. Unfortunately, after the login happens, I am getting only the user's email, display name, photo url and phone number. I saw that I can add scopes to the GoogleSignIn object, which I do - https://www.googleapis.com/auth/user.birthday.read, https://www.googleapis.com/auth/userinfo.profile, but still, I don't see any additional data after the login. Any idea how to get the result of this? Because when these scopes are added to the object, it asks me if I accept to give this data before doing the login, so I guess there should be a way to fetch and display them.

Here is my login code:

  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;
  }

回答1:


I hope this becomes useful to anyone else facing the same trouble :)

 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=AIzaSyB9n6b3P_JDcVolJNYsDxokc4qf5B-q4Ac",
      headers: {
        "Authorization": headers["Authorization"]
      }
    );
    final response = JSON.jsonDecode(r.body);
    return response["genders"][0]["formattedValue"];
  }



来源:https://stackoverflow.com/questions/59844578/get-users-birthday-gender-using-google-sign-in-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!