Created
January 2, 2014 03:35
-
-
Save aug2uag/8214700 to your computer and use it in GitHub Desktop.
Google OAuth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - (void)goGoogle | |
| { | |
| GPPSignIn *signIn = [GPPSignIn sharedInstance]; | |
| signIn.delegate = self; | |
| signIn.shouldFetchGoogleUserEmail = YES; | |
| signIn.clientID = GOOGLE_CLIENTID; | |
| signIn.scopes = [NSArray arrayWithObjects:kGTLAuthScopePlusLogin,nil]; | |
| signIn.actions = [NSArray arrayWithObjects:@"http://schemas.google.com/ListenActivity",nil]; | |
| [signIn authenticate]; | |
| } | |
| - (void)finishedWithAuth: (GTMOAuth2Authentication *)auth | |
| error: (NSError *) error | |
| { | |
| NSLog(@"Received error %@ and auth object %@",error, auth); | |
| NSString *urlStr = @"https://www.googleapis.com/oauth2/v1/userinfo?alt=json"; | |
| NSURL *url = [NSURL URLWithString:urlStr]; | |
| NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; | |
| [auth authorizeRequest:request | |
| completionHandler:^(NSError *error) { | |
| NSString *output = nil; | |
| if (error) { | |
| output = [error description]; | |
| } else { | |
| // Synchronous fetches like this are a really bad idea in Cocoa applications | |
| // | |
| // For a very easy async alternative, we could use GTMHTTPFetcher | |
| NSURLResponse *response = nil; | |
| NSData *data = [NSURLConnection sendSynchronousRequest:request | |
| returningResponse:&response | |
| error:&error]; | |
| if (data) { | |
| // API fetch succeeded | |
| output = [[NSString alloc] initWithData:data | |
| encoding:NSUTF8StringEncoding]; | |
| } | |
| NSLog(@"output:%@",output); | |
| } | |
| }]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment