GoogleDrive API文件上传未显示erros尚未上传到连接的驱动器

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

我正在考虑将我的Google Drive连接到我的应用程序,以便我可以将文件上传到其中。

I遵循了文档,并且已经在Google Cloud Portal上设置了我的应用程序,创建了我的OAuth客户端ID和我的服务帐户,以便能够将其连接到我的应用程序。在使用Postman和我的客户ID进行测试时,我可以将文件上传到驱动器,但是文件名没有与我的文件一起发送。在解决问题一段时间以来,我决定继续在应用程序中实施该问题,而是看到甚至在我的Google Drive中上传的文件也不是一个文件。

以下是将上传文件的方法: public async Task DriveUploadBasic(string filePath) { try { // Load pre-authorized user credentials from the environment. string keyPath = $"{Directory.GetCurrentDirectory()}\\googleapi1.json"; GoogleCredential credential = GoogleCredential.FromFile(keyPath) .CreateScoped(DriveService.Scope.Drive) .CreateScoped(DriveService.Scope.DriveFile); // Create Drive API service. var service = new DriveService(new BaseClientService.Initializer { HttpClientInitializer = credential, ApplicationName = "TestingApp1" }); //service.HttpClient.Timeout = TimeSpan.FromMinutes(2); // Upload file photo.jpg on drive. var fileMetadata = new Google.Apis.Drive.v3.Data.File() { Name = "test1.csv" }; FilesResource.CreateMediaUpload request; // Create a new file on drive. using (var stream = new FileStream(filePath, FileMode.Open)) { // Create a new file, with metadata and stream. request = service.Files.Create(fileMetadata, stream, "text/csv"); request.Fields = "id"; var progress = await request.UploadAsync().ConfigureAwait(false); if (progress.Status == UploadStatus.Failed) { Log($"Upload failed: {progress.Exception}"); } var file = request.ResponseBody; //Log(request.ResponseBody.ToString()); // Prints the uploaded file id. } } catch (Exception e) { if (e is AggregateException) { Log("Credential Not found"); } else if (e is FileNotFoundException) { Log("File not found"); } else { Log(e.Message); } } }

任何帮助很棒,谢谢!

当Debuggin这是我的发现时:

连接是通过服务创建的,没有例外

上传文件正在读取(所有882个字节)
    我的请求告诉我,该过程已经完成,所有882个字节均已发送
  • 响应主体为我提供了一个新ID,该ID应该是我驱动器中文件的ID。
  • -尽管一切看起来都不错,但我的驱动器中没有文件。
  • 可能的问题:
  • 我的流正在给出读写超时错误,但是由于我的文件正在读取并且所有字节被要求发送,所以我不对任何内容都没有任何关系。此错误可能是由于Internet连接不一致造成的。 任何帮助很棒,谢谢!

    我有同样的问题,所以我找到了你的帖子。我不知道你是否仍然需要帮助,我发现了问题。 情况是Google使服务帐户成为一个真实的Google帐户,因此它使得进度成为其他帐户尝试将文件添加到您的Google帐户中。
  • 希望这可以帮助您!对不起,英语不是我的母语,我的打字看起来很奇怪:(

c# .net google-api google-drive-api
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.