2016-02-22 198 views
4

我正在从谷歌驱动器下载文件。但是我使用google SDK面临很多问题。在developer.google.com中,有一些示例可用。如何使用目标c从谷歌驱动器下载文件?

GTLServiceDrive *drive = ...; 

    GTLDriveFile *file = ...; 

    GTMHTTPFetcher *fetcher = [drive.fetcherService fetcherWithURLString:file.downloadUrl];` 

    `[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) { 
     if (error == nil) { 
     NSLog(@"Retrieved file content"); 
     // Do something with data 
     } else { 
     NSLog(@"An error occurred: %@", error);  
     } 
    }]; 

我从developer.google.com网站上下载新的SDK,但在上面的例子中给出GTMHTTPFetcher *fetcher = [drive.fetcherService fetcherWithURLString:file.downloadUrl];。但在新的SDK中,在此GTLDriveFile中没有downloadURL对象。

而在另一个例子中,它要求客户端密钥,但是当我选择iOS类型时,没有客户端密钥的选项。但是,当我选择网络时,它正在显示。但它不起作用。

请帮助我如何使用目标c从iOS谷歌驱动器下载文件。

+0

根据文档,有一个'downloadURL'对象。请仔细检查您是否使用正确的资源。请参阅['downloadURL'](https://developers.google.com/drive/ios/reference/ios-client/interface_g_t_l_drive_file.html#a957260925b906876ccaf3598d24fc1dc) – gerardnimo

+0

Hi gerardnimo,我从doveloper.google.com网站下载了新的SDK。我检查了两次,GTLDriveFile中没有可用的downloadURL对象。在文档中可用,但我认为它的旧文档。请帮我解决这个问题。我正在尝试最后5天。仍然没有任何解决方案。 –

回答

0

从谷歌文档:

https://developers.google.com/drive/v2/reference/files/get

+ (void)downloadFileContentWithService:(GTLServiceDrive *)service 
            file:(GTLDriveFile *)file 
         completionBlock:(void (^)(NSData *, NSError *))completionBlock { 
    if (file.downloadUrl != nil) { 
    // More information about GTMHTTPFetcher can be found on 
    // <a href="http://code.google.com/p/gtm-http-fetcher">http://code.google.com/p/gtm-http-fetcher</a> 
    GTMHTTPFetcher *fetcher = 
     [service.fetcherService fetcherWithURLString:file.downloadUrl]; 

    [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) { 
     if (error == nil) { 
     // Success. 
     completionBlock(data, nil); 
     } else { 
     NSLog(@"An error occurred: %@", error); 
     completionBlock(nil, error); 
     } 
    }]; 
    } else { 
    completionBlock(nil, 
        [NSError errorWithDomain:NSURLErrorDomain 
             code:NSURLErrorBadUrl 
            userInfo:nil]); 
    } 
} 

这可能是旧的文档,所以看看这个答案:

Value of type 'GTLDriveFile' has no member 'downloadUrl'

+0

嗨大卫,这是旧的文档。在New SDK中,documentURL对象在GTLDriveFile中不可用。这就是为什么我无法找到解决方案。如果你能找到,请帮助我。 –

1

这可能会帮助你,你需要添加关键(iOS的API密钥)参数URL

GTLDriveFile *myfile;//Your File Object 
    NSString *url = [NSString stringWithFormat:@"https://www.googleapis.com/drive/v3/files/%@?key=YOUR_KEY_iOS", 
        myfile.identifier]; 
    GTMSessionFetcher *fetcher = [self.service.fetcherService fetcherWithURLString:url]; //GTLServiceDrive *service; 

    [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) { 
     if (error == nil) { 
      NSLog(@"Retrieved file content"); 
      // File Downloaded! 
     } else { 
      NSLog(@"An error occurred: %@", error); 
     } 
    }]; 
+0

我不确定是否需要在URL中添加密钥(是否指授权令牌?)。在API文档(https://developers.google.com/drive/ios/devguide/files)中,未提及。 – Etienne

相关问题