几年来,我有一个应用程序,用户可以使用它从gDrive加载数据。数据可以来自本地Google电子表格(gSheets)或txt,csv,xlsx等文件。突然以来,尽管本周没有任何变化,但本周有时突然无法下载文件。当我尝试下载文件时,我现在获得了html页面的内容,该页面基本上是一个登录屏幕。但是,仍然可以下载 gSheets的内容,所以这不可能是由于我正在使用的服务帐户的某些访问问题所致。我还可以确认文件仍与该服务帐户共享。 我正在使用以下代码下载文件:
String serviceAccountEmail = this.apiConfig.ClientIdEmail;
Byte[] keyFile = this.apiConfig.FileP12;
var certificate = new X509Certificate2(keyFile, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { DriveService.Scope.Drive, DriveService.Scope.DriveFile }
}.FromCertificate(certificate));
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Drive Spotfire Datasource",
});
var response = service.HttpClient.GetByteArrayAsync(exportUrl).Result;
Stream downloadStream = new MemoryStream(response);
“ exportUrl”的格式为
https://docs.google.com/a/<domain.com>/uc?id=<fileId>&export=download&gid=0
,当我在浏览器中尝试该网址时,可以毫无问题地下载文件。 在此期间,我尝试使用NuGet(版本1.38)中针对Google.Apis.Drive.v2和Google.Apis.Drive.v3的最新dll。 有谁知道为什么这些下载突然无法正常工作?
答案 0 :(得分:0)
与此同时,我找到了解决方案。事实证明,下载文件时需要使用另一个URL:
String serviceAccountEmail = this.apiConfig.ClientIdEmail;
Byte[] keyFile = this.apiConfig.FileP12;
var certificate = new X509Certificate2(keyFile, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { DriveService.Scope.Drive, DriveService.Scope.DriveFile }
}.FromCertificate(certificate));
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Drive Spotfire Datasource",
});
//get file handler
Google.Apis.Drive.v2.Data.File file = service.Files.Get(this.uniqueKey).Execute();
if (!file.MimeType.Equals("application/vnd.google-apps.spreadsheet"))
{
//for files use another Url
exportUrl = file.DownloadUrl;
}
var response = service.HttpClient.GetByteArrayAsync(exportUrl).Result;
Stream downloadStream = new MemoryStream(response);