在Flutter中使用Google登录获取用户的生日/性别 [英] Get user's birthday/gender using Google Sign-In in Flutter
问题描述
我想使用Firebase Auth和Google登录获取用户的生日和性别.不幸的是,登录发生后,我仅收到用户的电子邮件,显示名称,照片url和电话号码.我看到可以将范围添加到GoogleSignIn对象中- https://www.googleapis.com/auth/user.birthday.read , https://www.googleapis.com/auth/userinfo.profile ,但登录后仍看不到任何其他数据.知道如何获得结果吗?因为将这些作用域添加到对象后,它会问我是否接受在登录之前提供此数据,所以我想应该有一种获取和显示它们的方法.
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.
这是我的登录代码:
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;
}
推荐答案
我希望这对遇到同样麻烦的其他人有用:)
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=",
headers: {
"Authorization": headers["Authorization"]
}
);
final response = JSON.jsonDecode(r.body);
return response["genders"][0]["formattedValue"];
}
这篇关于在Flutter中使用Google登录获取用户的生日/性别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!