首页 > 解决方案 > 创建多个文件夹后,Google Drive Api (.Net) 返回错误

问题描述

我正在 MVC .Net(VS 2019 + Google Drive Api v3)中开发一个网络应用程序。我们正在使用一个服务帐户来使用一个简单的谷歌帐户中的共享文件夹。我们需要为学生的每项任务创建一个文件夹。我们有 2000 名学生,他们平均每月有 10 个任务。所以我们的谷歌驱动器中会有很多文件夹。因此,我决定使用控制台应用程序进行一些测试,并在创建多个文件夹后发现一些错误。有人知道 Google Drive Api 是否对创建多个文件夹有某种限制?下面是一个控制台应用程序,用打印图像证明这一点。

namespace DriveQuickstart
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] Scopes = { DriveService.Scope.DriveReadonly, DriveService.Scope.DriveFile };            
            ServiceAccountCredential credential;                                    
            using (var stream =
                            new FileStream("key.json", FileMode.Open, FileAccess.Read))
            {                            
                credential = GoogleCredential.FromStream(stream)
                                     .CreateScoped(Scopes)
                                     .UnderlyingCredential as ServiceAccountCredential;                
            }
            // Create Drive API service.
            var service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "Test App",
            });
            Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File();
            for (int i = 0; i < 50; i++)
            {
                body.Name = "Test_" + Guid.NewGuid().ToString();
                body.Description = "Created by me!";
                body.MimeType = "application/vnd.google-apps.folder";
                body.Parents = new List<string> { "1hLsDTub8bhlVS2ax34P8wGx5RsD0n8MA" };
                try
                {
                    service.Files.Create(body).Execute();
                    Console.WriteLine("Folder " + (i+1).ToString() + " created.");
                }
                catch (Exception e)
                {                    
                    Console.WriteLine("Error = " + e.InnerException.Message);
                }
            }                     
        }
    }
}

内部异常错误:“无法从传输连接读取数据。它被远程服务器强制取消”。

打印2

标签: google-apigoogle-drive-apigoogle-api-dotnet-clientgoogle-drive-realtime-api

解决方案


推荐阅读