【问题标题】:Using google drive sdk, unable to upload a file: Response coming to null使用 google drive sdk,无法上传文件:响应为空
【发布时间】:2016-02-08 19:21:59
【问题描述】:

我在下面有一个简单的代码可以上传到谷歌驱动器,但我每次都得到 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;
                }
            }

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

【问题讨论】:

    标签: .net c#-4.0 file-upload google-drive-api


    【解决方案1】:

    我能够解决这个问题。我没有通过足够的范围,而只是 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 }; 
    

    【讨论】:

    • 你能给我更多关于这个修复的细节吗?我被困在这个
    • @ASD 现在只需将范围分配给 all,如上所示,如果文档中的定义不明确,请根据需要逐一删除来查看您需要的范围。希望对您有所帮助!
    【解决方案2】:

    将文件上传到 Google Drive 需要您使用 Drive 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
    

    还有其他uploadTypes 可供选择,所以请选择对您最有利的东西。有关上传文件的更多信息,请访问V2 Official Documentation。他们的github repository 中也提供了示例代码(尽管这是在 JS 中)。

    希望这会有所帮助!

    【讨论】:

    • 感谢您的回复。我使用 API 在上面的代码中创建了一个发布请求。我得到响应= null。我想通过上面的代码来做到这一点。知道我在哪里做错了吗?
    猜你喜欢
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    相关资源
    最近更新 更多