【问题标题】:how to download file from google drive using objective c?如何使用objective c从谷歌驱动器下载文件?
【发布时间】:2016-02-22 05:30:39
【问题描述】:

我正在处理从谷歌驱动器下载文件。但是我在使用 google SDK 时遇到了很多问题。在 developer.google.com 中有一些可用的示例。

 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 中的谷歌驱动器下载文件。

【问题讨论】:

  • 根据文档,有一个downloadURL 对象。请仔细检查您是否使用了正确的资源。见downloadURL
  • 嗨 gerardnimo,我已经从 doveloper.google.com 网站下载了新的 SDK。我已经检查了两次,GTLDriveFile 中没有可用的 downloadURL 对象。在文档中有可用的,但我认为它的旧文档。请帮我解决这个问题。我正在尝试最后 5 天。仍然没有任何解决方案。

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


【解决方案1】:

这可能会帮助您在 URL 中添加密钥(iOS 的 API 密钥)参数

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

【讨论】:

  • 我不确定是否需要在 url 中添加密钥(您的意思是授权令牌吗?)。在 API 文档 (developers.google.com/drive/ios/devguide/files) 中没有提及。
  • 嗨,在我的情况下,这个 fetcher 服务不再工作了。您能帮我了解如何使用 GoogleSDK 下载大文件吗
【解决方案2】:

来自谷歌文档:

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'

【讨论】:

  • 嗨,大卫,这是旧文档。在 GTLDriveFile 中,新 SDK 中的 documentURL 对象不可用。这就是为什么我无法找到解决方案。如果你能找到,请帮助我。
猜你喜欢
  • 2018-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多