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?