【问题标题】:Google Drive API - list specific files in a specific folder using Objective-CGoogle Drive API - 使用 Objective-C 列出特定文件夹中的特定文件
【发布时间】:2013-02-26 16:26:23
【问题描述】:

我正在开发适用于 iOS 的 Google 云端硬盘集成,但遇到了问题 - 我需要检查文件夹中是否存在文件,但无论如何我都会收到“500 内部错误”响应我似乎把请求放在一起。我认为这应该是合法的:

// self.directoryDriveFile.identifier is a valid folder identifier retrieved on previous request
GTLQueryDrive *query = [GTLQueryDrive queryForChildrenListWithFolderId:self.directoryDriveFile.identifier];

// add query to specify the file we're interested in (works in Java version...)
query.q = [NSString stringWithFormat:@"title='%@' and trashed=false",
           ZTCloudSyncLockFileName];
// stopping in debugger shows query.q: title='sync.lock' and trashed=false

// fire off request
[self.driveService executeQuery:query
              completionHandler:^(GTLServiceTicket *ticket,
                                  GTLDriveFileList *files,
                                  NSError *error) {
                  if (error == nil) {
                      if ([files.items count] > 0) {
                          self.foundLockfile = YES;
                      } else {
                          self.foundLockfile = NO;
                      }
                  } else {
                      DLog(@"An error occurred loading lockfile request: %@", error);
                      [self bailWithError:error performCleanup:NO];
                  }
              }];

执行查询时,总是以错误告终,不幸的是,错误信息稀疏如下:

Error Domain=com.google.GTLJSONRPC
ErrorDomain Code=500 "The operation couldn’t be completed. (Internal Error)" 
UserInfo=0x99c4660 {
  error=Internal Error, 
  GTLStructuredError=GTLErrorObject 0x99c4360: {
  message:"Internal Error" code:500 data:[1]}, 
  NSLocalizedFailureReason=(Internal Error)}

我还尝试了以下更基本的查询,指定了 parents 子句,但不幸的是,我最终得到了上面显示的相同的备用错误对象:

GTLQueryDrive *altQuery = [GTLQueryDrive queryForFilesList];
altQuery.q = [NSString stringWithFormat:@"title='%@' and trashed=false and '%@' in parents",
              ZTCloudSyncLockFileName,
              self.directoryDriveFile.identifier];

这也应该可以,但也会产生 500 错误。


附加信息:在处理此问题时测试了以下内容:

  • 检查根目录中的文件夹—确定
  • 在根目录中创建文件夹—确定
  • 检查根目录中名为 sync.lock 的文件—确定

【问题讨论】:

  • 您使用的是服务帐号吗?见:stackoverflow.com/questions/13693617/…
  • 我不明白你的意思。看了一下链接,但没有解释“服务帐户”。我按照 obj-c 入门教程和其他示例中描述的方式使用 API。
  • @BillyGray 你应该像我一样在你的评论中包含目标人,否则他不会收到通知,也可能不会回来回复你的评论。
  • @ilmiacs 感谢您的提示!太糟糕了,移动网站上没有自动完成功能(目前无论如何)。干杯。
  • 看起来这可能是与标题查询与 drive.file 范围相结合的错误,而不是 Objective-c 代码(看起来不错)

标签: objective-c google-drive-api google-drive-realtime-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);
        }
    }];
}

您可以使用上述方法来获取文件夹内容。 这里的 folderId 是

  • “root”(主驱动器)
  • 共享文件夹的“sharedWithMe”
  • GTLDrivefile 标识符值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多