Ios – Using the Google APIs with OAuth 2.0 for gmail login in iPhone

gmailgoogle apiiosoauth

I have found a services from Google which provides to access to Google APIs for various Google Services. I could set up a project in iPhone and create API access for iOS applications (via OAuth2.0) and native applications. I wanted to use the native API for my iPhone app. It API gives me email,fullname,firstname,lastname,google_id,gender,dob,profile_image. How do I use these in my iPhone Application, Any sample apps, snippets available?

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's 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];
}

Best Answer

First you will need to get token from Google API, For this 1st step you will have to follow this tutorial and in the end of this link there is whole source code for iOS for getting 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 have to send that token to Google API to request user Data, I just needed the first step So I am sharing my searchings

Related Topic