Using the Google APIs with OAuth 2.0 to Log in to gmail on iPhone

I found services from Google that provide access to Google APIs for various Google services. I could create a project on iPhone and create API access for iOS apps (via OAuth2.0 ) and native apps. I wanted to use my own API for my iPhone application. It API gives me email, full name, first name, last name, google_id, gender, dob, profile_image. How can I use them in my application for iPhone, any sample applications available snippets?

Please help me.

Here is my code:

 -(void) loadGmail_Login { NSString *keychainItemName = nil; if ([self shouldSaveInKeychain]) { keychainItemName = kKeychainItemName; } // For GTM applications, the scope is available as NSString *scope = @"http://www.google.com/m8/feeds/"; // ### Important ### // GTMOAuthViewControllerTouch is not designed to be reused. Make a new // one each time you are going to show it. // Display the autentication view. GTMOAuthAuthentication *auth; auth = [GTMOAuthViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName]; GTMOAuthViewControllerTouch *viewController = [[[GTMOAuthViewControllerTouch alloc] initWithScope:scope language:nil appServiceName:keychainItemName delegate:self finishedSelector:@selector(viewController:finishedWithAuth:error:)] autorelease]; // You can set the title of the navigationItem of the controller here, if you want. // Optional: display some html briefly before the sign-in page loads NSString *html = @"<html><body bgcolor=silver><div align=center>Loading sign-in page...</div></body></html>"; [viewController setInitialHTMLString:html]; [[self navigationController] pushViewController:viewController animated:YES]; } - (void)viewController:(GTMOAuthViewControllerTouch *)viewController finishedWithAuth:(GTMOAuthAuthentication *)auth error:(NSError *)error { if (error != nil) { // Authentication failed (perhaps the user denied access, or closed the // window before granting access) NSLog(@"Authentication error: %@", error); NSData *responseData = [[error userInfo] objectForKey:@"data"]; // kGTMHTTPFetcherStatusDataKey if ([responseData length] > 0) { // show the body of the server authentication failure response NSString *str = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease]; NSLog(@"%@", str); } [self setAuthentication:nil]; } else { // save the authentication object [self setAuthentication:auth]; // Just to prove we're signed in, we'll attempt an authenticated fetch for the // signed-in user [self doAnAuthenticatedAPIFetch]; } } - (void)doAnAuthenticatedAPIFetch { NSString *urlStr; // Google Contacts feed // // https://www.googleapis.com/oauth2/v2/userinfo urlStr = @"http://www.google.com/m8/feeds/contacts/default/thin"; NSURL *url = [NSURL URLWithString:urlStr]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [mAuth authorizeRequest:request]; NSError *error = nil; NSURLResponse *response = nil; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if (data) { // API fetch succeeded NSString *str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]; NSLog(@"API response: %@", str); GGCXml_Adaptor *localAlphabetXMLParser = [[GGCXml_Adaptor alloc] init]; [localAlphabetXMLParser processBooksXML:data]; [localAlphabetXMLParser release]; // [self updateUI]; } else { // fetch failed NSLog(@"API fetch error: %@", error); } } - (void)setAuthentication:(GTMOAuthAuthentication *)auth { [mAuth autorelease]; mAuth = [auth retain]; } 
+7
source share
3 answers

First you will need to get the token from the Google API. For this 1st step, you will need to follow this guide, and at the end of this link there is a whole source code for iOS to get the token from google API

http://technogerms.com/login-with-google-using-oauth-2-0-for-ios-xcode-objective-c/

Then in the next step you need to send this token to the Google API to request user data, I just need the first step. So I'm sharing my quest

+2
source
+1
source

I think this will help someone else. Follow the instructions below to integrate gmail with your application.

1. Add the following classes to the project.

GTMHTTPFetcher.h, GTMHTTPFetcher.m, GTMOAuth2Authentication.h, GTMOAuth2Authentication.m, GTMOAuth2SignIn.h, GTMOAuth2SignIn.m, GTMOAuth2ViewControllerTouch.h, GTMOAuth2ViewControlMJTh.m.bh.m.

you will get the following classes: https://github.com/jonmountjoy/Force.com-iOS-oAuth-2.0-Example

Note If you are working in an ARC environment, you need to disable ARC for the following files:
GTMHTTPFetcher.m, GTMOAuth2Authentication.m, GTMOAuth2SignIn.m, GTMOAuth2ViewControllerTouch.m

To disable ARC for source files in Xcode 4, select the project and target in Xcode. Under the Phase Build tab, expand the compile source assembly phase, select the library source files, then press Enter to open the edit box and enter fno-objc-arc as the compiler flag for these files.

2. add the following frames

 security.framework , systemConfiguration.framework 

3. Register your application in the google api console .... here: https://code.google.com/apis/console

Then go to the ApiAccess section, create a client identifier for the iOS application. then you will get clientID, ClientSecret and RedirectUrl

** 4. Now it is time for coding.,. **
create a signIn button in the controller and set the action for this. Here, when the user clicks the button, the calling method SignInGoogleButtonClicked is called.

 //import GTMOAuth2Authentication , GTMOAuth2ViewControllerTouch #define GoogleClientID @"paster your client id" #define GoogleClientSecret @"paste your client secret" #define GoogleAuthURL @"https://accounts.google.com/o/oauth2/auth" #define GoogleTokenURL @"https://accounts.google.com/o/oauth2/token" -(void) SignInGoogleButtonClicked { NSURL * tokenURL = [NSURL URLWithString:GoogleTokenURL]; NSString * redirectURI = @"urn:ietf:wg:oauth:2.0:oob"; GTMOAuth2Authentication * auth; auth = [GTMOAuth2Authentication authenticationWithServiceProvider:@"google" tokenURL:tokenURL redirectURI:redirectURI clientID:GoogleClientID clientSecret:GoogleClientSecret]; auth.scope = @"https://www.googleapis.com/auth/plus.me"; GTMOAuth2ViewControllerTouch * viewcontroller = [[GTMOAuth2ViewControllerTouch alloc] initWithAuthentication:auth authorizationURL:[NSURL URLWithString:GoogleAuthURL] keychainItemName:@"GoogleKeychainName" delegate:self finishedSelector:@selector(viewController:finishedWithAuth:error:)]; [self.navigationController pushViewController:viewcontroller animated:YES]; } //this method is called when authentication finished - (void)viewController:(GTMOAuth2ViewControllerTouch * )viewController finishedWithAuth:(GTMOAuth2Authentication * )auth error:(NSError * )error { if (error != nil) { UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error Authorizing with Google" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } else { UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert !" message:@"success" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } } 
0
source

All Articles