[英]Get user's birthday/gender using Google Sign-In in Flutter
我想使用 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"];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.