google-api-dotnet-client - 从 Google API 随机错误下载 Excel - 响应状态代码不表示成功
问题描述
尝试从 Google Drive 下载 Excel (*.xlsx) 文件。
调用 DriveService.HttpClient.GetByteArrayAsync() 时出现此错误:
System.Net.Http.HttpRequestException 响应状态码不表示成功:500(Internal Server Error
- 此错误随机发生
- 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
解决方案
推荐阅读
- javascript - REACT JS:如何在地图内向尚未启动的数组添加键?
- solidity - 从 ERC271 合约继承有什么意义?
- c# - asp-for 标签助手显示“CS1061”错误
- flutter - 如何向免费用户显示不同的页面,向付费用户显示不同的页面
- r - 使用 Rselenium 进行网页抓取并保存为数据框时创建“for”循环
- alexa - Alexa 按时讲话的技巧是什么?
- android - 应用打开时单击后 Azure 推送通知未关闭
- php - Google oauth 致命错误:count() 使用 php 8.0.9 版本
- python - 我的代码正在独立运行,但不会在我的整个项目中运行/打印
- javascript - web 应用程序上的 react-chatbot-kit