Google OAuth error -1001

亡梦爱人 提交于 2020-01-11 12:25:30

问题


In my application i have try to implement the google account access, and when i initialise its working till in the login session. after which it throws the following error in the screen shot

Here my code Initialisation and method implementation

 static NSString *const kKeychainItemName =nil;
 NSString *kMyClientID = @"465568347336.apps.googleusercontent.com";     
 NSString *kMyClientSecret = @"rKVsWXTlo3M8zqNfofkX0Xrl"; 
 NSString *scope = @"https://www.googleapis.com/auth/userinfo.profile"; 

 GTMOAuth2ViewControllerTouch *viewController;
 viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:scope
                                                            clientID:kMyClientID
                                                        clientSecret:kMyClientSecret
                                                    keychainItemName:kKeychainItemName
                                                            delegate:self finishedSelector:@selector(viewController:finishedWithAuth:error:)];
 [self.navigationController presentModalViewController:viewController animated:YES];

Error handler

 - (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
  finishedWithAuth:(GTMOAuth2Authentication *)auth
             error:(NSError *)error {
 if (error != nil) {
    NSString *output=nil;
    output = [error description];
    NSLog(@"output:%@",output);
    UIAlertView *fail = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                   message:[NSString stringWithFormat:@"Error, Authentication failed!\n %@",error]
                                                  delegate:self
                                         cancelButtonTitle:@"OK"
                                         otherButtonTitles:@"Try again", nil];
    fail.tag = 1;

    [fail show];
    NSLog(@"Authentication failed!");
  } else {
    UIAlertView *success = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                      message:[NSString stringWithFormat:@"Authentication succeeded!"]
                                                     delegate:self
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];
    success.tag = 2;

    [success show];
    NSLog(@"Autzentication succeeded!");
   }

How to solve this is issue.Please help me to solve


回答1:


I implemented my GTMOAuth2 using the following codes and it worked for me, I hope it may help you in some way or another.

- (GTMOAuth2Authentication * )authForGoogle
{    
    NSURL * tokenURL = [NSURL URLWithString:GoogleTokenURL];

    NSString * redirectURI = @"urn:ietf:wg:oauth:2.0:oob";

    _auth = [GTMOAuth2Authentication authenticationWithServiceProvider:@"Google"
                                                         tokenURL:tokenURL
                                                      redirectURI:redirectURI
                                                         clientID:GoogleClientID
                                                     clientSecret:GoogleClientSecret];
    _auth.scope = @"https://www.googleapis.com/auth/userinfo.profile";
    return _auth;
}


- (void)signInToGoogle
{   
    _auth = [self authForGoogle];

    // Display the authentication view
    GTMOAuth2ViewControllerTouch * viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithAuthentication:_auth
                                                                                                 authorizationURL:[NSURL URLWithString:GoogleAuthURL]
                                                                                            keychainItemName:@"GoogleKeychainName"
                                                                                                    delegate:self
                                                                                            finishedSelector:@selector(viewController:finishedWithAuth:error:)];
    [_window setRootViewController: viewController];
    [_window makeKeyAndVisible];
}

 - (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)auth
         error:(NSError *)error 
{
    if (error != nil) {
       NSString *output=nil;
       output = [error description];
       NSLog(@"output:%@",output);
       UIAlertView *fail = [[UIAlertView alloc] initWithTitle:@"Alert"
                                               message:[NSString stringWithFormat:@"Error, Authentication failed!\n %@",error]
                                              delegate:self
                                     cancelButtonTitle:@"OK"
                                     otherButtonTitles:@"Try again", nil];
       fail.tag = 1;

       [fail show];
       NSLog(@"Authentication failed!");
    } else {
       UIAlertView *success = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                  message:[NSString stringWithFormat:@"Authentication succeeded!"]
                                                 delegate:self
                                        cancelButtonTitle:@"OK"
                                        otherButtonTitles:nil];
       success.tag = 2;

       [success show];
       NSLog(@"Autzentication succeeded!");
    }
}

I copied your finishedWithAuth method as it is similar to my implementation. I don't think there is much difference between our codes with regards to the implementation of GTMOAuth2 in xcode, but one thing that I realised when I was using GTMOAuth2 was that it is extremely hard to debug any errors that you face. I had this similar error to you as well, and realised that it was because I chose the wrong type of app in the Google portal when setting up the app and getting the clientID and clientSecret. I set it up as an iOS app at first (of course!), but then after reading different answers and problems online, I realised I should have created it as an Other app type. That solved my issue. Maybe you can check it out.

They have a forum where the support is quite comprehensive, this is the link here

To further add on, I can refer you to the tutorial that I referenced when I was integrating the GTMOAuth2 into my app. This is the link here.

Also, as I was developing an enterprise app that required me to check the email address of the user, it was very difficult for me to get the email even after the user was authenticated. I had to hack and read into the codes before I got what I needed, and if you need it in future, you can check my answer here on SO.

Hope this helps! :)




回答2:


Error Domain=com.google.GTMOAuth2 Code=-1001 usually happens if the user logs in, but in the OAuth agreement window (where it says " wants to access your emails" for example) the users clicks on "Cancel" or "No thanks".

So basically there is no way to "solve" this. You can handle it or just progress in your app without that services.



来源:https://stackoverflow.com/questions/19095861/google-oauth-error-1001

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!