首页 > 解决方案 > 从 Google API 随机错误下载 Excel - 响应状态代码不表示成功

问题描述

尝试从 Google Drive 下载 Excel (*.xlsx) 文件。

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

System.Net.Http.HttpRequestException 响应状态码不表示成功:500(Internal Server Error

我创建了一个似乎可以进行 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

标签: google-api-dotnet-client

解决方案


推荐阅读