无法使用 Google 服务帐户将文件上传到 Google 云端硬盘

问题描述 投票:0回答:1

我在将文件上传到 Google 云端硬盘时遇到问题。

我创建了一个服务帐户,将 google Drive api 添加到项目中,共享一个文件夹(该文件将发送)到服务帐户 ID。

我做了所有这些事情,但无法上传文件。

The service drive has thrown an exception. HttpStatusCode is NotFound. File not found: 1JlGO5gas321321(testId).

此服务帐户的访问级别是贡献者。当我在免费版本的谷歌驱动器(带有访问编辑器)上测试此解决方案时,一切正常。

我为此使用.net 6。

这是我的代码:

   public static async Task<UploadStatus> SaveToGoogleCloud(string csvLines, string fileTitle,string driveFolderId)
   {
       var loggedCredential = GoogleAuthentication.Login();
       using var driveService = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = loggedCredential });

       var csvMetadata = new Google.Apis.Drive.v3.Data.File()
       {
           Name = fileTitle,
           Parents = new List<string>() { driveFolderId },
       };

       var fsSource = new MemoryStream(Encoding.UTF8.GetBytes(csvLines ?? ""));

       var request = driveService.Files.Create(csvMetadata, fsSource, "text/plain");
       request.Fields = "*";
       var results = await request.UploadAsync();

       return results.Status;
   }

也许有人有类似的问题并且知道如何解决?

c# .net google-api google-drive-api
1个回答
0
投票

好吧,忽略我最后的答案,显然我不应该再尝试凭记忆

您介意在返回之前添加此内容吗,以便我们可以尝试调试此内容

    // Make the asynchronous call to upload the file.
    var results = await request.UploadAsync(CancellationToken.None);

    // Handle upload status.
    if (results.Status == UploadStatus.Failed)
    {
        // TODO: handle a file upload error.
        Console.WriteLine($"Error uploading file: {results.Exception.Message}");
    }

    var fileId = request.ResponseBody?.Id;
    Console.WriteLine($"fileId {fileId}" );
© www.soinside.com 2019 - 2024. All rights reserved.