【问题标题】:Google Drive API Upload file failed using Google Drive SDK in .NET在 .NET 中使用 Google Drive SDK 上传文件失败
【发布时间】:2018-12-26 04:33:28
【问题描述】:

我尝试将音频文件上传到谷歌驱动器。这是我的源代码:

   public override async void UploadLocalFileToDrive(StorageFile localFile, string driveFolderName)
    {
        if (localFile != null)
        {
            string folderId = await GetFolderIdByNameAsync(driveFolderName);
            if(!string.IsNullOrEmpty(folderId))
            {
                var uploadFile = new Google.Apis.Drive.v3.Data.File();
                uploadFile.Name = localFile.Name;
                uploadFile.Parents = new List<string>() { folderId };
                uploadFile.MimeType = "application/vnd.google-apps.audio";

                Stream stream = await localFile.OpenStreamForReadAsync();

                try
                {
                    var request = driveService.Files.Create(uploadFile, stream, uploadFile.MimeType);
                    await request.UploadAsync();
                    Debug.WriteLine(request.ResponseBody.Id);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
        }
    }

调用此函数时,没有文件上传到驱动器和对象

 request.ResponseBody

为空。

我正在为 UWP .Net 使用 Google.Apis.Drive.v3 库

我该如何解决?谢谢!

【问题讨论】:

  • 你能发布用户认证的代码吗?或者你还没有这样做吗
  • 因为 UWP 不完全支持 Google.Apis.Drive.v3 库,所以我的用户身份验证代码这么久。我之前有过用户身份验证,所以我可以通过 GetFolderIdByNameAsync 函数获取文件夹 ID。用户认证范围为googleapis.com/auth/drive
  • 它在official docs 上看起来很受支持,虽然我自己没有机会使用它。那么 GetFolderIdByNameAsync() 调用返回一个成功的值?
  • 是的。结果是一个成功的值。我可以通过将文件的 MimeType 更改为audio/mpeg 来解决这个错误。我在下面的答案中写了它。感谢您对我的问题感兴趣。

标签: .net uwp google-drive-api


【解决方案1】:

我在我的代码中发现了一个错误。我使用 appliction/vnd.gooogle-apps.audio 作为文件的 MimeType。它必须是audio/mpeg。感谢您的关注!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多