0
votes

I have trouble using [self isAuthorized] to get confirmation of the access token I've got earlier.

Every time I'm login in with Google Drive SDK for iOS with:

// Creates the auth controller for authorizing access to Google Drive.
-(GTMOAuth2ViewControllerTouch *)createAuthController {

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

After the authentification completed, there is no error so the access token should be saved correctly

// Handle completion of the authorization process, and updates the Drive service
// with the new credentials.
-(void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)authResult error:(NSError *)error {
    if (error != nil)
    {
        //[self showAlert:@"Authentication Error" message:error.localizedDescription];
        self.driveService.authorizer = nil;
    }
    else
    {
        self.driveService.authorizer = authResult;
    }
}

I used an NSLog to make sure I received the access token and It did.

-(BOOL)isAuthorized {

    NSString *oauthToken = [((GTMOAuth2Authentication *)self.driveService.authorizer) accessToken];
    NSLog(@"oauthToken: %@", oauthToken);


    return [((GTMOAuth2Authentication *)self.driveService.authorizer) canAuthorize];
}

But When I look if I'm authorized or not, there is no token saved (oauthToken is NULL) and I need to login again.

N.B: It was working in the past before iOS 9. I don't know if it is related.

Thanks Vincent

1

1 Answers

1
votes

You might want to try something like this to verify login:

// Check for authorization.
GTMOAuth2Authentication *auth =
[GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName
                                                      clientID:kClientId
                                                  clientSecret:kClientSecret];
if ([auth canAuthorize]) {
    [self isAuthorizedWithAuthentication:auth];
}

On the other hand, if you really want the access token, check out this SO post, however, it is not best practice to store the access token, since access token has an expiration time. Good luck & Hope this helps.