使用 google drive sdk,无法上传文件:响应为空

Using google drive sdk, unable to upload a file: Response coming to null

我在下面有一个简单的代码可以上传到 google 驱动器,但我每次都得到 response = null。我的身份验证有效,我可以列出文件但不能上传或创建目录。 我需要一些额外的权限吗?我已经看到了一些示例,我正在关注它们,但我的上传仍然无法正常工作。

如果有人能纠正我,那将是很大的帮助。下面是我的上传代码。

public static Google.Apis.Drive.v2.Data.File uploadFile(DriveService service, string uploadFile, string parent = null)
            {
                if (System.IO.File.Exists(uploadFile))
                {                
                    Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
                    body.Title = System.IO.Path.GetFileName(uploadFile);
                    body.Description = "File uploaded by installed app";
                    body.MimeType = GetMimeType(uploadFile);
                    //body.Parents = new List<ParentReference>() { new ParentReference() { Id = parent } };

                    // File's content
                    byte[] byteArray = System.IO.File.ReadAllBytes(uploadFile);
                    System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

                    try
                    {
                        FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, GetMimeType(uploadFile));
                        //request.Convert = true;
                        request.Upload();
                        Google.Apis.Drive.v2.Data.File f = request.ResponseBody;
                        Console.WriteLine(f.Id);
                        return f;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("An error occured: " + ex.Message);
                        return null;
                    }
                }
                else
                {
                    Console.WriteLine("File does not exist: " + uploadFile);
                    return null;
                }
            }

需要这方面的指导,如果有人能帮助我,我将不胜感激!

将文件上传到 Google 驱动器需要您使用驱动器 REST API。由于您已经拥有身份验证令牌,因此您只需调用 POST URL 和其他需要设置的参数即可。

一个简单的上传案例如下所示

POST /upload/drive/v2/files?uploadType=media HTTP/1.1
Host: www.googleapis.com
Content-Type: image/jpeg
Content-Length: number_of_bytes_in_file
Authorization: Bearer your_auth_token

JPEG data

还有其他 uploadType 可供选择,因此请选择对您最有利的内容。有关上传文件的更多信息,请参见 V2 Official Documentation. A sample code is also provided in their github repository(尽管这是在 JS 中)。

希望对您有所帮助!

我能够解决这个问题。我没有通过足够的范围,只是 DriveService.Scope.Drive。我从 string[] scopes = new string[] { DriveService.Scope.Drive }; 更改为

string[] scopes = new string[] { DriveService.Scope.Drive,  
                           DriveService.Scope.DriveAppdata,
                           DriveService.Scope.DriveAppsReadonly,      
                           DriveService.Scope.DriveFile,   
                           DriveService.Scope.DriveMetadataReadonly, 
                           DriveService.Scope.DriveReadonly,      
                           DriveService.Scope.DriveScripts };