【问题标题】:How to download pdf from google drive V3 API in IOS?如何在 IOS 中从 google drive V3 API 下载 pdf?
【发布时间】:2016-05-20 06:50:49
【问题描述】:

使用Google Drive V3 API 下载文件和pdf。

  1. 根据 Google Doc,下面的 Google Drive V3 Api 是下载文件(比如文本文件)的 URL。

    NSString *url = [NSString stringWithFormat:@"https://www.googleapis.com/drive/v3/files/%@?alt=media",file.identifier];

但是,当我简单地使用此 url 时,它在下载文件时给了我错误,然后我尝试使用客户端 ID 进行类似的操作并且它工作正常。(这里我删除了 alt=media 并在 url 中添加了客户端 ID。这是完美的工作正常)。下面是修改后的网址。

`NSString *url = [NSStringstringWithFormat:@"https://www.googleapis.com/drive/v3/files/%@?key=%@", file.identifier,kClientID];`
  1. 现在他们在 Google Doc 中提到使用以下网址的 pdf。

    NSString *url = [NSString stringWithFormat:@"https://www.googleapis.com/drive/v3/files/%@/export?alt=media&mimeType=application/pdf", file.identifier];

我再次面临同样的问题..上面下载 pdf 的 url 给了我错误。我已经完成了与 url 的所有排列和组合,但没有成功。

***文档中提供的示例代码使用的是google drive V2 Api。

那么,如何使用 Google Drive V3 Api 下载 pdf?请帮助。

【问题讨论】:

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


    【解决方案1】:

    今天,我从 Google Drive V3 Api 下载文件成功。

    self.fileSize = [loadFile.quotaBytesUsed unsignedIntegerValue];
    
            ////////////////////////////////////////////////////////////////////////////////////////////////////
            GTMSessionFetcher *fetcher = [GTMSessionFetcher fetcherWithURLString:loadFile.webContentLink];
            if(fetcher==nil)
            {
                break;
            }
    
            fetcher.authorizer             = [GTLServiceDrive sharedServiceDrive].authorizer;
            fetcher.destinationFileURL     = [NSURL fileURLWithPath:self.intoPath];
    
            __block typeof(self) blockSelf = self;
            fetcher.downloadProgressBlock  = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
                //get download progress
            };
    
            ////////////////////////////////////////////////////////////////////////////////////////////////////
            [fetcher beginFetchWithDelegate:self didFinishSelector:@selector(fetcher:finishedWithData:error:)];
    

    【讨论】:

    • 这也可以在我看来显示进度。如果还有其他问题可以告诉我。谢谢。
    • @Andrew Lai,sharedServiceDrive 实例方法不在 Api 中。你从哪里得到这个?
    • GTLDriveFile *file=[dataArr objectAtIndex:indexPath.row]; NSString *url = [NSString stringWithFormat:@"googleapis.com/drive/v3/files/%@/…", file.identifier] GTMSessionFetcher *fetcher = [self.service.fetcherService fetcherWithURLString:url]; [fetcher beginFetchWithCompletionHandler:^(NSData data, NSError error) { if (error == nil) { NSLog(@"Retrieved file content"); // 文件已下载! }];
    • @samar,通过使用 GTLServiceDrive 并设置以下两个属性。 shouldFetchNextPages = YES, 授权者由 GTMOAuth2ViewControllerTouch 的方法 authForGoogleFromKeychainForName:clientID:clientSecret: 设置
    • 我总是得到 loadFile.webContentLink nil。
    【解决方案2】:

    -(void)downloadFile:(NSString *)url{

    GTMSessionFetcher *fetcher = [GTMSessionFetcher fetcherWithURLString:url];
    fetcher.authorizer             = [[GTLServiceDrive alloc]init].authorizer;
    fetcher.destinationFileURL     = [NSURL fileURLWithPath:url];
    
    __block typeof(self) blockSelf = self;
    fetcher.downloadProgressBlock  = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
        //get download progress
        NSLog(@"bytesWritten = %d",bytesWritten);
         NSLog(@"totalBytesWritten = %d",totalBytesWritten);
         NSLog(@"totalBytesExpectedToWrite = %d",totalBytesExpectedToWrite);
    
    };
    
    [fetcher beginFetchWithDelegate:self didFinishSelector:@selector(fetcher:finishedWithData:error:)];
    

    }

    【讨论】:

    • 我在“didSelectRowAtIndexPath”中调用此方法,url 为“file.webContentLink”。当我运行我的代码时,错误 "Error Domain=NSCocoaErrorDomain Code=518 "文件 "docs.google.com" 无法保存,因为 URL 类型 https 不受支持。" UserInfo={NSURL=https:/docs. google.com/}”
    • 我已经阅读了你所有的源代码。您必须使用本地文件路径 url 设置 destinationFileURL,与 fetcherWithURLString 的 url 不同。我也回复你的邮件。
    猜你喜欢
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多