使用 Google API for .NET 从 gDrive 下载文件时出现问题

Issue with downloading files from gDrive with Google API for .NET

几年前我就有一个应用程序,用户可以使用它从 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

当我在浏览器中尝试 Url 时,我可以毫无问题地下载文件。 同时,我尝试使用来自 NuGet(版本 1.38)的最新 dll,用于 Google.Apis.Drive.v2 和 Google.Apis.Drive.v3。 有谁知道为什么这些下载突然不能用了?

同时我找到了解决办法。原来下载文件的时候还需要用到另一个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);