Google Drive API - 使用Objective-C列出特定文件夹中的特定文件

时间:2013-02-26 16:26:23

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

我正在开发适用于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错误。


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

  • 检查目录root-OK中的文件夹
  • 在目录root-OK
  • 中创建文件夹
  • 在目录root-OK
  • 中检查名为sync.lock的文件

1 个答案:

答案 0 :(得分:0)

-(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”
  • GTLDrive文件标识符值