在.NET中使用Google Drive SDK导致Google Drive API上传文件失败

时间:2018-07-18 09:51:06

标签: .net uwp google-drive-api

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

   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库

我该如何解决?谢谢!

1 个答案:

答案 0 :(得分:0)

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