我使用从谷歌自己的.NET quickstart复制的以下代码来列出您的Google Drive帐户中的文件(身份验证部分已调整为使用通过遵循here概述的步骤获得的刷新令牌)。
using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Responses;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using System;
using System.Diagnostics;
namespace GoogleTest {
class Program {
[STAThread]
static void Main(string[] args) {
var sw = Stopwatch.StartNew();
var secrets = new ClientSecrets() {
ClientId = "paste-your-client-id-here",
ClientSecret = "paste-your-client-secret-here"
};
var token = new TokenResponse { RefreshToken = "paste-your-refresh-token-here" };
var credentials = new UserCredential(new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer {
ClientSecrets = secrets
}),
"user",
token);
Console.WriteLine($"1: {sw.Elapsed}");
// Create the service.
var service = new DriveService(new BaseClientService.Initializer() {
HttpClientInitializer = credentials
});
Console.WriteLine($"2: {sw.Elapsed}");
// Define parameters of request.
var listRequest = service.Files.List();
listRequest.PageSize = 10;
listRequest.Fields = "nextPageToken, files(id, name)";
Console.WriteLine($"3: {sw.Elapsed}");
// List files.
var files = listRequest.Execute().Files;
Console.WriteLine($"4: {sw.Elapsed}");
if (files != null && files.Count > 0) {
foreach (var file in files) {
Console.WriteLine("{0} ({1})", file.Name, file.Id);
}
} else {
Console.WriteLine("No files found.");
}
Console.WriteLine($"5: {sw.Elapsed}");
}
}
}运行上面的代码会产生以下代码的输出:
1: 00:00:00.1075210
2: 00:00:00.1881154
3: 00:00:00.1987953
4: 00:00:12.5158776
No files found.
5: 00:00:12.5166778
Press any key to continue . . .换句话说,var files = listRequest.Execute().Files调用需要10秒或更长时间。通常情况下,这甚至需要20秒。
当然,对于如此琐碎的请求,这不可能是正常的响应时间。我是不是漏掉了什么?正如你所看到的,有问题的Google Drive帐户甚至不包含任何文件,并且是完全空的。
发布于 2021-04-06 23:38:25
Google Apis的响应时间取决于很多变量。
你需要记住,这是一个免费的api,你可以得到你想要的东西。是的,有时呼叫返回速度很慢,但您不会为此付费。
https://stackoverflow.com/questions/66913349
复制相似问题