Bhumika Bhumika - 5 months ago 100
iOS Question

How to get user birthday from google plus using oauth 2 api in ios?

I have implemented Google plus signIn in my app and now i want to get user birthday via Google OAuth api.I have already get user birthday using google developer playground.but now i want to know than how can i get user birthday in iOS(swift) programmatically?

Any help will be appreciated.

Answer

You can get birthdate from Google plus using oauth api in this way:

First of all you have to set scope to get birthdate

signIn.scopes = ["me", "profile"]

let url : String = "https://www.googleapis.com/plus/v1/people/me"
    let request : NSMutableURLRequest = NSMutableURLRequest()
    request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
    request.URL = NSURL(string: url)
    request.HTTPMethod = "GET"
    print(request)
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue()) { (response : NSURLResponse?, data:NSData?, error:NSError?) -> Void in
        do
        {
            let jsonResult : NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
            print("Birthdate   : \(jsonResult.valueForKey("birthday"))")
        }
        catch
        {
            print(error)
        }
    }
Comments