如何使用 Drive API 3.0 下载 Google 文档的修订版?

How can I download a Revision of a Google Doc using Drive API 3.0?

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

在 v2 中获得 Google 文档修订

  1. OAUTH2权限(使用的账号必须有Editor权限)

  2. 使用 FileId 和 RevId(此 returns 修订元数据)向驱动器 API 的版本 2 发送 GET 请求

  3. 使用在步骤 2 中为所需的导出类型(例如 docx(或 pdf/etc ),对于修订版(此 returns 一个非常长的 URI,很快就会过期)。

  4. 向临时“导出”URI 发送另一个 GET 请求,以获取步骤 2 中指定的 Revision 和导出类型的二进制文件流。

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

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

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