Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27133

iOS Google Drive API. The operation couldn’t be completed. (com.google.GTMOAuth2 error -1000.)

So everything works good and I like this sdk, but when I push auth controller:

GTMOAuth2ViewControllerTouch *authController;
    authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeDrive
                                                                clientID:kClientID
                                                            clientSecret:kClientSecret
                                                        keychainItemName:kKeychainItemName
                                                                delegate:self
                                                        finishedSelector:@selector(viewController:finishedWithAuth:error:)];

    [self.navigationController pushViewController:authController animated:YES];

and then go back without entering user e-mail and password the delegate callback return me error:

The operation couldn’t be completed. (com.google.GTMOAuth2 error -1000.)

But I am not sure that the delegate should return this error for me. I have used quick start example.

My question why it return error, is this correct. Because I just pop back from auth controller to previous controller there is nothing criminal and I don't want to process any api calls.

Upvotes: 3

Views: 970

Answers (1)

Anil
Anil

Reputation: 21910

The delegate method returns the error code -1000 in the NSError parameter when the window was prematurely closed.

This error.code (-1000) is nothing to worry about, it's there for you to see what the user did. If they did go back without authorizing you may want to display a message "You didn't authorize, to use our awesome features with Google you should!", or you can just simply ignore it.


from: /Project/Pods/gtm-oauth2/GTMOAuth2Authentication.h

enum {
  // Error code indicating that the window was prematurely closed
  kGTMOAuth2ErrorWindowClosed          = -1000,
  kGTMOAuth2ErrorAuthorizationFailed   = -1001,
  kGTMOAuth2ErrorTokenExpired          = -1002,
  kGTMOAuth2ErrorTokenUnavailable      = -1003,
  kGTMOAuth2ErrorUnauthorizableRequest = -1004
};

Copy and Paste Example:

A simple example for new iOS developers. (Google's documentation is also fantastic)
Install Google API via CocoaPods using (e.g: subset /Drive):

pod 'Google-API-Client/Drive'

In your view controller, add the following vars:

// access keys (Should be in a seperate class or constants file)
#define kKeychainItemName @"Google Drive Quickstart"
#define kGDClientID @"your-client-id"
#define kGDClientSecret @"your-client-secret"

@interface ClassViewController() {
    GTMOAuth2ViewControllerTouch *_authController;           // Auth v/c
    YourCustomGoogleDriveController *_googleDriveController; // Custom Class
}

Next we need a method to create and return our Google OAuth2 View Controller, add this into the same class.

/**
 * Create a new instance of our google auth view controller
 */
- (GTMOAuth2ViewControllerTouch *)createAuthController
{
    _authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeDrive
                                                                 clientID:kClientID
                                                             clientSecret:kClientSecret
                                                         keychainItemName:kKeychainItemName
                                                                 delegate:self
                                                         finishedSelector:@selector(viewController:finishedWithAuth:error:)];
    return _authController;
}

When the delegate method above completes, it will call the function below (notice the delegate: self and @selector in the above function).

/**
 * Our delegate method, always called, either with auth token or error
 */
- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController 
      finishedWithAuth:(GTMOAuth2Authentication *)authResult
                 error:(NSError *)error
{
    if (error != nil) {
        // We have some sort of error, handle it or ignore
        _googleDriveController.driveService.authorizer = nil;
        switch (error.code) {
            case kGTMOAuth2ErrorWindowClosed:
                // Show a message? : "Sign in for awesomeness!"
                NSLog(@"%ld: User closed window prematurely", (long)error.code);
                break;
            case kGTMOAuth2ErrorAuthorizationFailed:
                NSLog(@"%ld: Authorization Failed", (long)error.code);
                break;
            case kGTMOAuth2ErrorTokenExpired:
                NSLog(@"%ld: Token Expired", (long)error.code);
                break;
            case kGTMOAuth2ErrorTokenUnavailable:
                NSLog(@"%ld: Token Unavailable", (long)error.code);
                break;
            case kGTMOAuth2ErrorUnauthorizableRequest:
                NSLog(@"%ld: Unauthorizable Request", (long)error.code);
                break;
            default:
                break;
        }
    } else {
        // Set the result into our `GTLServiceDrive` in our Custom Controller
        _googleDriveController.driveService.authorizer = authResult;
        NSLog(@"Success! - User Auth'ed!");
    }
}

Finally we need a IBAction or something to call our above code:

/**
 * Create our auth controller, and push it to the nav stack (assuming we are in one)
 */
- (IBAction)someButtonClicked:(id)sender
{
    [self createAuthController]; // Init our auth controller
    _authController.title = @"Sign in"; // Set a custom title (if you want)
    [self.navigationController pushViewController:_authController animated:YES];
    //[self presentViewController:controller animated:YES completion:nil]; // Show Modally
}

Upvotes: 2

Related Questions