【问题标题】:How to get a list of folder and file Google Drive API IOS?如何获取文件夹和文件的列表 Google Drive API IOS?
【发布时间】:2013-03-18 12:01:05
【问题描述】:

我想使用 google drive API 从我的 google drive 中获取所有文件和文件夹名称。

我的查询是这样的:

 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);
                                            }];
                          }
                      }
                  }];
}

【问题讨论】:

  • Here 你可能会找到文档(包括 Objective C 代码 sn-p)和一个表单来创建请求并检查你是否使用了正确的参数
  • @A-live 帮到你了
  • 请自行回答或提供更多详细信息。

标签: ios objective-c google-drive-api


【解决方案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 可能是“root”(对于根文件夹),sharedWithMe(对于共享文件夹) .如果你想列出垃圾文件然后改变垃圾=真。

【讨论】:

  • 只是对@IOS 答案的更新:folderIdfile.identifier。如果要获取所有文件和文件夹,请使用“root”作为folderId。注意:记得使用kGTLAuthScopeDrive 而不是kGTLAuthScopeDriveFile
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多