gpt4 book ai didi

c# - 在 C# 控制台应用程序中获取 google 驱动程序中文件的 downloadUrl

转载 作者:行者123 更新时间:2023-11-30 17:30:18 26 4
gpt4 key购买 nike

我正在努力连接到谷歌驱动器,列出所有格式的每个文件,然后将其物理下载到我的硬盘上。我使用了 google API 为我准备的方法。但我认为我的工作方式只能访问每个文件的内容,所以我不能那样做。

你能帮我知道我需要哪个配置吗?

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!");


}
}

在这里,如果我可以访问每个文件的 downloadUrl,我可以测试另一种下载方式。但它是空的,这是我的下载方法:

public static void DownloadFile(DriveService 服务, File 文件, string saveTo) {

    var request = service.Files.Get(file.Id);
var stream = new System.IO.MemoryStream();
Console.WriteLine(file.FileExtension);


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);
break;
}
case DownloadStatus.Failed:
{
Console.WriteLine("Download failed.");
break;
}
}
};

try
{
request.Download(stream);
}
catch (Exception ex)

{
Console.Write("Error: " + ex.Message);
}

}

本次下载状态为失败。我还检查了范围,我发现这些行只允许我下载我在这个应用程序中创建的文件:

private static readonly string[] Scopes = new[] { DriveService.Scope.DriveFile, DriveService.Scope.Drive };

但我需要在列出所有文件后,才能下载每个文件。

能否请您帮我解决问题或其他工作代码?

最佳答案

您需要使用有效的文件名并处理 google doc 文件:

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

}

关于c# - 在 C# 控制台应用程序中获取 google 驱动程序中文件的 downloadUrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50176628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com