使用谷歌驱动器sdk,无法上传文件:回应为空 [英] Using google drive sdk, unable to upload a file: Response coming to null

查看:150
本文介绍了使用谷歌驱动器sdk,无法上传文件:回应为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



如果有人能纠正我的话,这将非常有帮助。下面是我的上传代码。

  public static Google.Apis.Drive.v2.Data.File uploadFile(DriveService service,string uploadFile ,字符串parent = null)
{
if(System.IO.File.Exists(uploadFile))
{
Google.Apis.Drive.v2.Data.File body =新的Google.Apis.Drive.v2.Data.File();
body.Title = System.IO.Path.GetFileName(uploadFile);
body.Description =通过安装的应用上传文件;
body.MimeType = GetMimeType(uploadFile);
//body.Parents = new List< ParentReference>(){new ParentReference(){Id = parent}};

//文件内容
byte [] byteArray = System.IO.File.ReadAllBytes(uploadFile);
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

尝试
{
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);
返回f;
}
catch(Exception ex)
{
Console.WriteLine(An error occured:+ ex.Message);
返回null;


else
{
Console.WriteLine(File does not exist:+ uploadFile);
返回null;






$ b

需要关于这方面的指导,如果有人可以帮助我在这里!

解决方案

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

 字符串[]范围=新字符串[] {DriveService.ScopeDrive,
DriveService.Scope.DriveAppdata,
DriveService.Scope.DriveAppsReadonly,
DriveService。 Scope.DriveFile,
DriveService.Scope.DriveMetadataReadonly,
DriveService.Scope.DriveReadonly,
DriveService.Scope.DriveScripts};


I have a simple code below to upload to google drive but i get response = null everytime. My authentication works and I am able to list the files but not upload or create directory. Do I need some extra permissions? I have seen some samples and I am following them but still my upload doesn't work.

It will be of great help if someone could correct me. Below is my upload code.

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;
                }
            }

Need guidance on this, I would really appreciate if someone could help me out here!

解决方案

I was able to solve this. I was not passing enough scopes but just DriveService.Scope.Drive. I changed from string[] scopes = new string[] { DriveService.Scope.Drive }; to

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 }; 

这篇关于使用谷歌驱动器sdk,无法上传文件:回应为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆