首页 > 解决方案 > 使用 REST API v3 使用 C# 导出 Google Apps 脚本项目

问题描述

我正在尝试从 Google Apps 脚本项目下载.gs.html文件;但是,由于文档不足,这被证明是一项艰巨的任务。

我目前正在使用 Drive Rest v3 API。

我不断收到的错误是:

服务驱动器抛出异常:

Google.GoogleApiException:Google.Apis.Requests.RequestError
不支持请求的转换。[400]
错误 [
        消息[不支持请求的转换。] Location[convertTo - parameter] Reason[badRequest] Domain[global]
]
   在 C:\Apiary\2018-05-30.11-21-16\Src\Support\Google.Apis\Download\MediaDownloader.cs: 323 中的 Google.Apis.Download.MediaDownloader.d__31.MoveNext()
Google.Apis.Download.MediaDownloader+DownloadProgress

我感觉 MIME 类型可能已关闭。这是代码。

DriveService ds = new DriveService(new BaseClientService.Initializer()
{
 HttpClientInitializer = credential,
 ApplicationName = ApplicationName,
});

FilesResource.ExportRequest exp = ds.Files.Export(id,"application/vnd.google-apps.script");

//exp.Alt = DriveBaseServiceRequest<string>.AltEnum.Json;
//FilesResource.GetRequest get = new FilesResource.GetRequest(ds, id);

var streamer = new System.IO.MemoryStream();

exp.MediaDownloader.ProgressChanged +=
(IDownloadProgress progress) =>
{
switch (progress.Status)
{
  case DownloadStatus.Downloading:
  {
    Console.WriteLine(progress.BytesDownloaded);
    break;
  }
  case DownloadStatus.Completed:
  { 
    Console.WriteLine("Download complete.");
    break;
  }
  case DownloadStatus.Failed:
  {
    Console.WriteLine("Download failed.");
    Console.WriteLine(progress.Exception);
    break;
  }
}
};
IDownloadProgress i = exp.DownloadWithStatus(streamer);

标签: c#google-apps-scriptgoogle-drive-apigoogle-api-dotnet-client

解决方案


谷歌在 2017 年推出了clasp,这是一个用于导入和导出应用程序脚本项目并在本地开发的官方工具。这是从 Google Apps 脚本项目下载 .gs 和 .html 文件的推荐方式。试试扣拉命令,效果很好!


推荐阅读