如何从Google登录中检索年龄和性别

我已将Google登录集成到我的iOS应用中。 我想访问用户的性别和年龄。 文档不够清晰,不知道如何做到这一点。 我发现我应该要求合适的范围。 我没有在文档中找到正式的范围列表,我不知道应该使用哪个范围。 我也不知道如何在获取数据时检索数据。 如果有人帮我从谷歌获取此信息,我将不胜感激。 谢谢!
这是我的代码:

func googleLogin() { self.appDelegate.setIdentityAvailableValue(false) GIDSignIn.sharedInstance().clientID = kClientId GIDSignIn.sharedInstance().shouldFetchBasicProfile = true GIDSignIn.sharedInstance().delegate = self GIDSignIn.sharedInstance().uiDelegate = self GIDSignIn.sharedInstance().signInSilently() } func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) { if (error == nil) { let idToken = user.authentication.idToken let url = NSURL(string: "https://www.googleapis.com/oauth2/v3/userinfo?access_token=\(user.authentication.accessToken)") let session = NSURLSession.sharedSession() session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in //UIApplication.sharedApplication().networkActivityIndicatorVisible = false if error != nil { print("dataTaskWithURL error \(error)") } else { do { let userData = try NSJSONSerialization.JSONObjectWithData(data!, options:[]) as? [String:AnyObject] /* Get the account information you want here from the dictionary Possible values are "id": "...", "email": "...", "verified_email": ..., "name": "...", "given_name": "...", "family_name": "...", "link": "https://plus.google.com/...", "picture": "https://lh5.googleuserco...", "gender": "...", "locale": "..." so in my case: */ let gender = userData!["gender"] as! String let locale = userData!["locale"] as! String print("gender = \(gender)") print("locale = \(locale)") } catch { NSLog("Account Information could not be loaded") } } }) } else { // some error handling code } } 

这是Swift 3

只需使用以下方法:

  func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) 

用户信息检索代码:并将此代码放在上面的方法中 –

 let gplusapi = "https://www.googleapis.com/oauth2/v3/userinfo?access_token=\(user.authentication.accessToken!)" let url = NSURL(string: gplusapi)! let request = NSMutableURLRequest(url: url as URL) request.httpMethod = "GET" request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type") let session = URLSession.shared session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) in UIApplication.shared.isNetworkActivityIndicatorVisible = false do { let userData = try JSONSerialization.jsonObject(with: data!, options:[]) as? [String:AnyObject] let picture = userData!["picture"] as! String let gender = userData!["gender"] as! String let locale = userData!["locale"] as! String } catch { NSLog("Account Information could not be loaded") } }).resume() 

你需要在最后调用resume(),否则关闭将不会被调用。我花了三天时间来解决这个问题。 所以我希望这会对某人有所帮助。

您只能访问用户设置为公开的内容。 如果用户不希望公开此信息,则您无法获得该信息。

 func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) { if (error == nil) { // Perform any operations on signed in user here. let userId = user.userID // For client-side use only! let idToken = user.authentication.idToken // Safe to send to the server let name = user.profile.name let email = user.profile.email let url = NSURL(string: "https://www.googleapis.com/oauth2/v3/userinfo?access_token=\(user.authentication.accessToken)") let session = NSURLSession.sharedSession() session.dataTaskWithURL(url!) {(data, response, error) -> Void in UIApplication.sharedApplication().networkActivityIndicatorVisible = false do { let userData = try NSJSONSerialization.JSONObjectWithData(data!, options:[]) as? [String:AnyObject] /* Get the account information you want here from the dictionary Possible values are "id": "...", "email": "...", "verified_email": ..., "name": "...", "given_name": "...", "family_name": "...", "link": "https://plus.google.com/...", "picture": "https://lh5.googleuserco...", "gender": "...", "locale": "..." so in my case: */ let gender = userData!["gender"] as! String let locale = userData!["locale"] as! String } catch { NSLog("Account Information could not be loaded") } } } else { print("\(error.localizedDescription)") } } func signIn(signIn: GIDSignIn!, didDisconnectWithUser user:GIDGoogleUser!, withError error: NSError!) { // Perform any operations when the user disconnects from app here. // ... }