在C#控制台应用程序中获取google驱动程序中文件的下载URL
我正在努力连接到谷歌硬盘,列出所有格式的文件,然后在我的硬盘上下载。我使用了谷歌API为我准备的方法。但我认为我的工作方式,我只能访问每个文件的内容,所以我不能这样做 你能帮我知道我需要哪种配置吗在C#控制台应用程序中获取google驱动程序中文件的下载URL,c#,google-drive-api,console-application,C#,Google Drive Api,Console Application,我正在努力连接到谷歌硬盘,列出所有格式的文件,然后在我的硬盘上下载。我使用了谷歌API为我准备的方法。但我认为我的工作方式,我只能访问每个文件的内容,所以我不能这样做 你能帮我知道我需要哪种配置吗 public static Google.Apis.Drive.v2.Data.FileList ListFiles(DriveService service, FilesListOptionalParms optional = null) { try
public static Google.Apis.Drive.v2.Data.FileList ListFiles(DriveService service, FilesListOptionalParms optional = null)
{
try
{
if (service == null)
throw new ArgumentNullException("service");
var request = service.Files.List();
request = (FilesResource.ListRequest)SampleHelpers.ApplyOptionalParms(request, optional);
return request.Execute();
}
catch (Exception ex)
{
throw new Exception("Request Files.List failed.", ex);
}
}
我调用此方法,对于我要在PC上下载的每个文件:
FileList files_list = DriveListExample.ListFiles(service, null);
if (files_list.Items.Count != 0)
{
foreach (var file in files_list.Items)
{
DriveListExample.DownloadFile(service, file, DownloadDirectoryName);
Console.WriteLine("\"{0}\" ", file.Title + " downloaded completely!");
}
}
在这里,如果我可以访问每个文件的下载URL,我可以测试另一种下载方式。但它是空的,这是我的下载方法:
公共静态void下载文件(DriveService服务、文件文件、字符串保存到)
{
在此过程中,下载的状态为失败。
我还检查了作用域,发现有了这些行,我就可以下载在此应用程序中创建的文件:
private static readonly string[] Scopes = new[] { DriveService.Scope.DriveFile, DriveService.Scope.Drive };
但我需要在列出所有文件后,我可以下载每个文件
您能帮我解决方案或其他工作代码吗?您需要使用有效的文件名并处理google文档文件:
public static void DownloadFile(DriveService service, Google.Apis.Drive.v2.Data.File file, string saveTo)
{
var request = service.Files.Get(file.Id);
var stream = new System.IO.MemoryStream();
// Add a handler which will be notified on progress changes.
// It will notify on each chunk download and when the
// download is completed or failed.
request.MediaDownloader.ProgressChanged += (IDownloadProgress progress) =>
{
switch (progress.Status)
{
case DownloadStatus.Downloading:
{
Console.WriteLine(progress.BytesDownloaded);
break;
}
case DownloadStatus.Completed:
{
Console.WriteLine("Download complete.");
SaveStream(stream, saveTo + @"/" + file.Title);
break;
}
case DownloadStatus.Failed:
{
if (file.ExportLinks.Any())
SaveStream(new HttpClient().GetStreamAsync(file.ExportLinks.FirstOrDefault().Value).Result, saveTo + @"/" + file.Title);
Console.WriteLine("Download failed.");
break;
}
}
};
try
{
request.Download(stream);
}
catch (Exception ex)
{
Console.Write("Error: " + ex.Message);
}
}
尝试在作用域中再添加一个作用域“DriveService.scope.DriveReadonly”。@Adrian但我认为这并不能解决问题。它只是访问文件。我想下载它们。
public static void DownloadFile(DriveService service, Google.Apis.Drive.v2.Data.File file, string saveTo)
{
var request = service.Files.Get(file.Id);
var stream = new System.IO.MemoryStream();
// Add a handler which will be notified on progress changes.
// It will notify on each chunk download and when the
// download is completed or failed.
request.MediaDownloader.ProgressChanged += (IDownloadProgress progress) =>
{
switch (progress.Status)
{
case DownloadStatus.Downloading:
{
Console.WriteLine(progress.BytesDownloaded);
break;
}
case DownloadStatus.Completed:
{
Console.WriteLine("Download complete.");
SaveStream(stream, saveTo + @"/" + file.Title);
break;
}
case DownloadStatus.Failed:
{
if (file.ExportLinks.Any())
SaveStream(new HttpClient().GetStreamAsync(file.ExportLinks.FirstOrDefault().Value).Result, saveTo + @"/" + file.Title);
Console.WriteLine("Download failed.");
break;
}
}
};
try
{
request.Download(stream);
}
catch (Exception ex)
{
Console.Write("Error: " + ex.Message);
}
}