I followed http://googlemac.blogspot.com/2011/05/ios-and-mac-sign-in-controllers.html to allow users to use Google to login to an iPhone app. After I tap "Allow access" button I get an extra screen that says, "Please copy this code, switch to your application and paste it there: (code in a textbox)."
This is what I have:
- (IBAction)googleLoginTapped:(UIButton *)sender
{
[self loginToGoogle];
}
- (void)loginToGoogle
{
// For Google APIs, the scope strings are available
// in the service constant header files.
NSString *scope =@"https://www.googleapis.com/auth/userinfo.profile";
// Typically, applications will hardcode the client ID and client secret
// strings into the source code; they should not be user-editable or visible.
// But for this sample code, they are editable.
NSString *clientID = @"my clientID";
NSString *clientSecret = @"my clientSecret";
// Display the autentication view.
SEL finishedSel = @selector(viewController:finishedWithAuth:error:);
GTMOAuth2ViewControllerTouch *viewController;
viewController = [GTMOAuth2ViewControllerTouch controllerWithScope:scope
clientID:clientID
clientSecret:clientSecret
keychainItemName:nil
delegate:self
finishedSelector:finishedSel];
// For this sample, we'll force English as the display language.
NSDictionary *params = [NSDictionary dictionaryWithObject:@"en"
forKey:@"hl"];
viewController.signIn.additionalAuthorizationParameters = params;
// 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.initialHTMLString = html;
viewController.signIn.shouldFetchGoogleUserProfile = YES;
[self presentModalViewController:viewController animated:YES];
}
- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error
{
if (error != nil)
{
// Authentication failed (perhaps the user denied
please see this link it is to good https://developers.google.com/accounts/docs/OAuth2InstalledApp
It turns out this is pretty straightforward. In the login callback, simply dismiss and remove
viewController
from the parent view controller.When using gtm-oauth2 to sign in to Google services, be sure the Google API Console project registration shows in the API Access section that the Client ID is issued for an installed application. This is described in the gtm-oauth2 documentation.
I tried this approach, and work fine, put this in your webViewDidFinishLoad method :
After at least 20 hours of configuring things, I finally got this running. I also previously imported my swift file into my GTMOAuth2ViewControllerTouch.m file. Not sure if that impacted it but I added:
Then, in the viewController.swift file I needed to add the final 2 lines:
That got rid of the copy this code screen.
I fixed this by nesting the view controller inside of a
UINavigationController
. No idea why did that did the trick, but it did.So instead of
... use
while creating Client ID , Choose web Application instead of installed application , this will solve your problem .
happy coding :)