2012-07-04 28 views
3

我正在尝试使用oauth 2.0协议来连接google文档。我认为连接是好的,因为我得到了访问令牌。之后,我想列出文件。我将gdata for objective-c api添加到了我的项目中,并遵循了这些示例,但我没有收到任何文档。我只是想读取firt的文档标题并显示它,但是一定是错误的,或者我错过了一些东西。任何帮助?谢谢。下面是代码:试图用oauth和gdata列出google文档objective-c api

ViewController.m

@implementation ViewController 

@synthesize accessToken; 
@synthesize mDocListFeed; 
@synthesize mDoclistFetchTicket; 


static NSString *const kMyClientID = @"199740745364-22lugf8undgv0rc0ucbfpgsn3v90lfsd.apps.googleusercontent.com"; 
static NSString *const kMyClientSecret = @"dPFs5D66kLyQIgUNL6igKUoX"; 
static NSString *const kKeychainItemName = @"casa"; 

- (GDataServiceGoogleDocs *)docsService { 

    static GDataServiceGoogleDocs* service = nil; 

    if (!service) { 
    service = [[GDataServiceGoogleDocs alloc] init]; 

    [service setShouldCacheResponseData:YES]; 
    [service setServiceShouldFollowNextLinks:YES]; 
    [service setIsServiceRetryEnabled:YES]; 
} 

return service; 

} 



- (void) mifetch { 

    GDataServiceGoogleDocs *service = [self docsService]; 

    GDataServiceTicket *ticket; 


    NSURL *feedURL = [GDataServiceGoogleDocs docsFeedURL]; 


    ticket = [service fetchFeedWithURL:feedURL 
       delegate:self 
     didFinishSelector:@selector(ticket:finishedWithFeed:error:)]; 

    mDoclistFetchTicket = ticket; 

} 

- (void) ticket: (GDataServiceTicket *) ticket 
     finishedWithFeed: (GDataFeedDocList *) feed 
      error: (NSError *) error { 

mDocListFeed = feed; 


GDataEntryDocBase *doc = [[mDocListFeed entries] objectAtIndex:0]; 

NSString *ttitle = [[doc title] stringValue]; 
UIAlertView *alertView = [ [UIAlertView alloc] initWithTitle:@"primer doc" 
            message:[NSString stringWithFormat:@"titulo: %@", ttitle] 
                delegate:self 
              cancelButtonTitle:@"Dismiss" 
              otherButtonTitles:nil]; 

[alertView show]; 

} 


- (void)authorize { 

    NSString *scope = @"https://spreadsheets.google.com/feeds"; 

// scope for Google+ API 

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


[[self navigationController] pushViewController:windowController animated:YES]; 
} 


- (IBAction)autenticarse 
{ 
[self authorize]; 
} 


- (IBAction)listar 
{ 

[self mifetch]; 
} 


- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController 
    finishedWithAuth:(GTMOAuth2Authentication *)auth 
      error:(NSError *)error 
{ 


if (error != nil) 
{ 
    // Authentication failed 
    UIAlertView *alertView = [ [UIAlertView alloc] initWithTitle:@"Authorization Failed" 
                 message:[error localizedDescription] 
                 delegate:self 
               cancelButtonTitle:@"Dismiss" 
               otherButtonTitles:nil]; 
    [alertView show]; 
} 
else 
{ 
    //si error==nil en el callback, entonces la peticion fue autorizada 
    // Authentication succeeded 

    // Assign the access token to the instance property for later use 
    self.accessToken = auth.accessToken; 



    // Display the access token to the user 
    UIAlertView *alertView = [ [UIAlertView alloc] initWithTitle:@"Authorization Succeeded" 
    message:[NSString stringWithFormat:@"Access Token: %@ code:%@", auth.accessToken, auth.code] 
                 delegate:self 
               cancelButtonTitle:@"Dismiss" 
               otherButtonTitles:nil]; 
    [alertView show]; 
    [[self docsService] setAuthorizer:auth]; 

} 
} 


// table view data source methods 


//The first thing we have to do is, tell the table view how many rows it should expect and this is done in tableView:numberOfRowsInSection. 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

if (tableView == tablalistar) { 
    return [[mDocListFeed entries] count]; 
} 

return 0; 


} 


//Now that the table view knows how many rows to display, we need to display the actual text which goes in a table view cell. The table view is made of table rows and rows contains table cell. This is done in tableView:cellForRowAtIndexPath which is called n number of times, where n is the value returned in tableView:numberOfRowsInSection. The method provides indexPath which is of type NSIndexPath and using this we can find out the current row number the table view is going to display. 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 


UITableViewCell *cell = [tableView 
         dequeueReusableCellWithIdentifier:@"tablalistar"]; 

if (tableView == tablalistar) { 



    GDataEntryDocBase *doc = [[mDocListFeed entries] objectAtIndex:indexPath.row]; 

    cell.textLabel.text = [[doc title] stringValue]; 


} 
return cell; 
} 




@end 

谢谢!

+0

在控制台中是否有任何错误? – Lvsti

回答

1

范围字符串只请求电子表格API的权限,但取回的是文档列表API。

的文档列表API的范围可作为+[GDataServiceGoogleDocs authorizationScope]

作用域为多个服务可以与+[GTMOAuth2Authentication scopeWithStrings:]

顺便提及,文档列表API已取代的Google Drive API组合。

+0

非常感谢你!有用!!!! – user1502091

相关问题