How to retrieve Google Drive files using OAuth authentication in iOS with Objective-C

I’m working on an iOS app that needs to connect to Google Drive using OAuth 2.0. The authentication part seems to work fine since I can get the access token successfully. However, when I try to fetch and display the list of files, nothing shows up. I’m using the Google Data API for Objective-C and followed their documentation, but I must be missing something. I want to get the first document’s name and display it in an alert, but the code isn’t working as expected. Can someone help me figure out what’s wrong?

@implementation MainViewController

@synthesize authToken;
@synthesize driveFileFeed;
@synthesize fetchTicket;

static NSString *const kClientIdentifier = @"your-client-id.googleusercontent.com";
static NSString *const kClientKey = @"your-client-secret";
static NSString *const kKeychainName = @"myapp";

- (GDataServiceGoogleDocs *)googleService {
    static GDataServiceGoogleDocs* serviceInstance = nil;
    
    if (!serviceInstance) {
        serviceInstance = [[GDataServiceGoogleDocs alloc] init];
        [serviceInstance setShouldCacheResponseData:YES];
        [serviceInstance setServiceShouldFollowNextLinks:YES];
        [serviceInstance setIsServiceRetryEnabled:YES];
    }
    return serviceInstance;
}

- (void)fetchDocuments {
    GDataServiceGoogleDocs *service = [self googleService];
    NSURL *requestURL = [GDataServiceGoogleDocs docsFeedURL];
    
    GDataServiceTicket *requestTicket = [service fetchFeedWithURL:requestURL
                                                         delegate:self
                                                didFinishSelector:@selector(requestTicket:completedWithFeed:error:)];
    fetchTicket = requestTicket;
}

- (void)requestTicket:(GDataServiceTicket *)ticket
     completedWithFeed:(GDataFeedDocList *)feed
                 error:(NSError *)error {
    
    driveFileFeed = feed;
    
    if([[driveFileFeed entries] count] > 0) {
        GDataEntryDocBase *firstDoc = [[driveFileFeed entries] objectAtIndex:0];
        NSString *docTitle = [[firstDoc title] stringValue];
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"First Document"
                                                        message:[NSString stringWithFormat:@"Name: %@", docTitle]
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
}

- (IBAction)startAuth {
    NSString *authScope = @"https://docs.google.com/feeds";
    
    GTMOAuth2ViewControllerTouch *authController = [[GTMOAuth2ViewControllerTouch alloc]
                                                   initWithScope:authScope
                                                        clientID:kClientIdentifier
                                                    clientSecret:kClientKey
                                                keychainItemName:kKeychainName
                                                        delegate:self
                                                finishedSelector:@selector(authController:finishedWithAuth:error:)];
    
    [[self navigationController] pushViewController:authController animated:YES];
}

- (void)authController:(GTMOAuth2ViewControllerTouch *)controller
      finishedWithAuth:(GTMOAuth2Authentication *)authentication
                 error:(NSError *)error {
    
    if (error != nil) {
        UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Auth Failed"
                                                             message:[error localizedDescription]
                                                            delegate:self
                                                   cancelButtonTitle:@"OK"
                                                   otherButtonTitles:nil];
        [errorAlert show];
    } else {
        self.authToken = authentication.accessToken;
        [[self googleService] setAuthorizer:authentication];
        
        UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:@"Auth Success"
                                                               message:@"Connected successfully"
                                                              delegate:self
                                                     cancelButtonTitle:@"OK"
                                                     otherButtonTitles:nil];
        [successAlert show];
    }
}

@end

Any suggestions on what might be causing this issue?

Your problem is that the old Google Data API (GDataServiceGoogleDocs) has been deprecated since 2015. That’s probably why you’re getting zero results even with proper authentication. You need to switch to the Google Drive API v3 with the Google APIs Client Library for iOS. Your GTMOAuth2 setup can mostly remain the same, but change your scope to “https://www.googleapis.com/auth/drive.readonly” and replace GDataServiceGoogleDocs with GTLRDriveService. The API calls will also differ—use GTLRDriveQuery_FilesList for fetching files. I went through this same migration a few years back, and it resolved similar file retrieval issues.

Check your app permissions in Google Cloud Console. Even if auth works, you might have wrong scopes or your project isn’t set up for Drive access. Also, log the actual error object in your completion handler - you’re only checking feed count, but there might be an error you’re missing.

Your code looks fine, but you’re missing a key step. You authenticate successfully in authController:finishedWithAuth:error: and set up the service with the token, but you never actually call fetchDocuments. Just add [self fetchDocuments]; right after setting the authorizer (before or after your success alert). Without that call, you’re authenticated but never request the files. Also double-check you’re testing with a Google account that has documents in Drive - empty drives won’t return anything. I’d throw some error logging in your completion handler too, since authentication or permission issues might be failing silently.