【问题标题】:Google Drive API Http Client Timeout C#Google Drive API Http 客户端超时 C#
【发布时间】:2022-08-28 23:36:12
【问题描述】:

我目前正在开发一个 C# 软件,它会压缩一些文件,然后将它们上传到我的谷歌驱动器。

我遵循了 Google Drive API 的本教程:How to upload a file to Google Drive with C# .net

问题是上传失败,因为 HTTP 客户端超时。

有人知道吗?

是驱动器上文件创建中的“文本/纯文本”吗?

private static string pathToServiceAccountKeyFile = @"C:\Users\X\Downloads\File.json";
        private const string serviceAccountEmail = XXX";
        private static string uploadFileName = @$"{zipFilesStorageDir}\{date}.zip";
        private const string directoryID = "XXX";


private static void FileUpload()
        {
            uploadFileName = @$"{zipFilesStorageDir}\{date}.zip";


            // Load the Service account credentials and define the scope of its access.
            var credential = GoogleCredential.FromFile(pathToServiceAccountKeyFile)
                            .CreateScoped(DriveService.ScopeConstants.Drive);



            // Create the  Drive service.
            var service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential
            });


            // Upload file Metadata
            var fileMetadata = new Google.Apis.Drive.v3.Data.File()
            {
                Name = date + ".zip",
                Parents = new List<string> { directoryID }
            };





            string uploadedFileId;
            // Create a new file on Google Drive
            using (var fsSource = new FileStream(uploadFileName, FileMode.Open, FileAccess.Read))
            {
                // Create a new file, with metadata and stream.
                var request = service.Files.Create(fileMetadata, fsSource, "text/plain");
                request.Fields = "*";
                var results = request.Upload();

                if (results.Status == UploadStatus.Failed)
                {
                    Console.WriteLine($"Error uploading file: {results.Exception.Message}");
                }

                // the file id of the new file we created
                uploadedFileId = request.ResponseBody?.Id;
            }

【问题讨论】:

    标签: c# google-api google-drive-api google-api-dotnet-client


    【解决方案1】:

    本教程展示了如何上传文本文件,使其使用 mime 类型

    var request = service.Files.Create(fileMetadata, fsSource, "text/plain");
    

    对于上传 zip,我应该认为这可以解决问题。

    var request = service.Files.Create(fileMetadata, fsSource, "application/zip");
    

    来自File.create 的文档

    我认为正在发生的事情是你告诉它你正在上传一个文本文件,但你实际上正在上传一个 zip,所以这就是它不起作用的原因。但是我认为它抛出一个http客户端超时有点奇怪。

    如果这对你不起作用,请告诉我。

    【讨论】:

    • 我不确定是否修复了它,因为它仍然无法在我的电脑上运行。无论如何,问题似乎是我的机器/网络。我将程序上传到远程 PC 并在那里工作。不知道为什么,但我不会碰它。无论如何,感谢您的时间和帮助。编辑:我还评论了您在视频下回复的 Stackoverflow 链接,但它没有接受。为搜索服务
    • 检查您的防火墙,它可能会阻止呼叫
    • 已经在所有防火墙上尝试过,但它可能是我们的网络服务。
    猜你喜欢
    • 2018-07-11
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    相关资源
    最近更新 更多