【发布时间】:2021-04-02 01:14:57
【问题描述】:
我使用从 Google 自己的 .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 帐户甚至不包含任何文件并且完全为空。
【问题讨论】:
-
检查您的互联网连接,尝试 ping 服务器并查看您获得的响应时间?听起来像是网络问题而不是编程问题。但请记住,Google Workspace API 并不是世界上最快的(尽管我同意 10-20 秒太慢) - 我的意思是,它们可以免费使用,对吗?
-
据我所知,我的互联网连接很好。我在两台不同的机器上运行了测试……一台托管多个网站的服务器和我家的电脑。没什么大不了的,我猜我只是期望从谷歌服务中获得更多。
-
你有多少个文件?您可能只能检索 10 个,但云端硬盘会扫描您的所有文件,然后再决定返回哪 10 个。
标签: performance google-api google-drive-api google-oauth google-api-dotnet-client