从Google API下载Excel随机错误-响应状态代码不表示成功

时间:2019-06-20 04:09:56

标签: google-api-dotnet-client

尝试从Google云端硬盘下载Excel(* .xlsx)文件。

调用DriveService.HttpClient.GetByteArrayAsync()时出现此错误:

System.Net.Http.HttpRequestException 响应状态代码不表示成功:500(内部服务器错误

  • 此错误是随机发生的
  • Excel文件的大小从0.5 MB到15 MB
  • 一个文件似乎更经常出错。是4 MB
  • 这是每个月左右随机发生的。从本周开始,几乎每当我运行下载18个Excel文件的代码时,都会发生这种情况。

我创建了一个捕获/重试机制,该机制似乎可以尝试3次。我仍然想要修复

byte[] byteArray = Encoding.UTF8.GetBytes(map.Credentials);
string[] scopes = new string[] { DriveService.Scope.Drive, DriveService.Scope.DriveFile };
UserCredential credential;
using (var stream = new MemoryStream(byteArray))
{
    string credPath = "token.json";
    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.Load(stream).Secrets,
        scopes,
        "user",
        CancellationToken.None,
        new FileDataStore(credPath, true)).Result;
}
DriveService driveService = new DriveService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = ApplicationName,
});

string path = "https://www.googleapis.com/drive/v3/files/" + map.Path + "?alt=media";
var x = driveService.HttpClient.GetByteArrayAsync(path);
byte[] arrBytes = x.Result;  // Randomly throws exception

0 个答案:

没有答案