2013-03-18 268 views
0

我想使用谷歌驅動器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

回答

1
-(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

相關問題