如何使用Drive API 3.0下载Google文档的修订版? [英] How can I download a Revision of a Google Doc using Drive API 3.0?

查看:49
本文介绍了如何使用Drive API 3.0下载Google文档的修订版?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于有了c#.NET中Drive API 2.0(v2)的有效解决方案,可以使用以下过程获取Google Doc Revision二进制流.获取文档修订的过程与仅获取普通文档的过程大不相同.

I finally have a working solution for Drive API 2.0 (v2) in c# .NET to get a Google Doc Revision binary stream using the process below. This process for getting a Document Revision is very different from just getting a normal Document.

要在v2中获得Google文档修订

  1. OAUTH2授权(所使用的帐户必须具有编辑者权限)

  1. OAUTH2 authorization (the account used must have Editor permissions)

使用FileId和RevId将GET请求发送到Drive API的第2版(这将返回Revision元数据)

Send GET request to version 2 of the Drive API with the FileId and RevId (this returns Revision metadata)

使用"export"将另一个GET请求发送到Drive API的版本2.在第2步中为修订版本所需的导出类型(例如docx(或pdf/etc))获得的URI(这将返回一个很长的URI,并且很快就会过期).

Send another GET request to version 2 of the Drive API with "export" URI obtained in step 2 for the desired export type, such as docx (or pdf/etc), for the Revision (this returns a massively long URI that expires quickly).

将另一个GET请求发送到临时的导出"窗口,URI,以获取步骤2中指定的修订和导出类型的二进制文件流.

Send another GET request to the temporary "export" URI to get the binary file stream of the Revision and export type specified in step 2.

有没有一种方法可以在Drive 3.0(v3)中获取Google Doc Revision的二进制流?此功能似乎已被删除.C#或JavaScript或curl或PHP或任何编码语言都可以...

Is there a way to get the binary stream of a Google Doc Revision in Drive 3.0 (v3)? This feature seems to have been removed. C# or JavaScript or curl or PHP or any coding language is fine...

推荐答案

以下是从受保护的Google文档获取修订的解决方案.注意:OAUTH2流中的用户必须对文档具有编辑"权限.这使用了前端解决方案,并进行了以下修改.

Here is the solution to get a Revision from a Google Doc that is secured. Note: The user in the OAUTH2 flow must have Edit permissions on the doc. This uses a front end solution with the modification below.

   private async Task GetRevisionStream(string accessToken, string fileId, string revId)
    {
        Log("Making API Call to get revision binary stream...");

        // builds the  request
        string userinfoRequestUri = "https://docs.google.com/feeds/download/documents/export/Export?id=" + fileId + "&revision=" + revId + "&exportFormat=docx";

        // sends the request
        HttpWebRequest userinfoRequest = (HttpWebRequest)WebRequest.Create(userinfoRequestUri);
        userinfoRequest.Method = "GET";
        userinfoRequest.Headers.Add(string.Format("Authorization: Bearer {0}", accessToken));
        userinfoRequest.ContentType = "application/x-www-form-urlencoded";
        userinfoRequest.Accept = "Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

        // gets the response
        WebResponse userinfoResponse = await userinfoRequest.GetResponseAsync();
        using (StreamReader userinfoResponseReader = new StreamReader(userinfoResponse.GetResponseStream()))
        {
            // reads response body
            System.IO.Stream streamDoc = userinfoResponseReader.BaseStream;
            var fileStream = File.Create("d:\\test.docx");
            streamDoc.CopyTo(fileStream);
            fileStream.Flush();
            fileStream.Close();
        }
    }

这篇关于如何使用Drive API 3.0下载Google文档的修订版?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆