如何获取Google Drive API的文件夹和文件列表?

问题描述:

我想使用谷歌驱动器API从我的谷歌驱动器获取所有文件和文件夹名称。如何获取Google Drive API的文件夹和文件列表?

我的查询是这样的:

GTLQueryDrive *query = [GTLQueryDrive queryForFilesList]; 

    query.q = @""; 
    //or i also use this code 

    query.q = @"mimeType = 'text/plain'"; 

连我也试过这样的代码:

-(void)getFileListFromSpecifiedParentFolder { 
    GTLQueryDrive *query2 = [GTLQueryDrive queryForChildrenListWithFolderId:@"root"]; 
    query2.maxResults = 1000; 

    // queryTicket can be used to track the status of the request. 
    [self.driveService executeQuery:query2 
        completionHandler:^(GTLServiceTicket *ticket, 
             GTLDriveChildList *children, NSError *error) { 
         NSLog(@"\nGoogle Drive: file count in the folder: %d", children.items.count); 
         //incase there is no files under this folder then we can avoid the fetching process 
         if (!children.items.count) { 
          return ; 
         } 

         if (error == nil) { 
          for (GTLDriveChildReference *child in children) { 

           GTLQuery *query = [GTLQueryDrive queryForFilesGetWithFileId:child.identifier]; 

           // queryTicket can be used to track the status of the request. 
           [self.driveService executeQuery:query 
              completionHandler:^(GTLServiceTicket *ticket, 
                   GTLDriveFile *file, 
                   NSError *error) { 

               NSLog(@"\nfile name = %@", file.originalFilename); 
              }]; 
          } 
         } 
        }]; 
} 
+0

[这里](https://developers.google.com/drive/v2/reference/children/list)如果你使用你会发现文件(包括目标C代码段)和一个表单创建请求,并检查正确的参数 – 2013-03-18 13:08:21

+0

@ A-live它的帮助完整 – ios 2013-03-19 04:45:06

+0

随时自己回答或提供更多的细节。 – 2013-03-19 05:59:54

-(void)fetchGoogleDriveFileListWithfolderId:(NSString *)folderId 
    :(void (^)(NSMutableArray *, NSError*))completionBlock 
{ 
    GTLQueryDrive *query = [GTLQueryDrive queryForFilesList]; 
    query.q =[NSString stringWithFormat:@"'%@' in parents and trashed=false", folderId]; 

    GTLServiceTicket *ticketListing = [self.driveService 
     executeQuery:query completionHandler:^(GTLServiceTicket *ticket,GTLDriveFileList *files, NSError *error) 
    { 
     NSMutableArray *mainArray=[[NSMutableArray alloc]init]; 

     if (error == nil) 
     { 
      completionBlock(files.items,nill); 
     } 
     else 
     { 
      NSLog(@"An error occurred: %@", error); 
      completionBlock(nil,error); 
     } 
    }]; 
} 

这里query.q =[NSString stringWithFormat:@"'%@' in parents and trashed=false", folderId];

folderId可能 “根”(对于根文件夹) ,sharedWithMe(用于共享文件夹) 。如果要列出已删除的文件,请更改tra棚= TRUE。

+0

只是一个更新@ IOS的​​回答是:'folderId'是' file.identifier'。如果你想获得所有的文件和文件夹,然后使用“root”作为'folderId'。注意:请记住使用'kGTLAuthScopeDrive'而不是'kGTLAuthScopeDriveFile'。 – Tim 2016-06-30 03:43:52