ArgumentNullException:“baseUri”将文件上传到Google云端硬盘时为空 [英] ArgumentNullException: "baseUri" is null, when uploading a file to Google Drive

查看:112
本文介绍了ArgumentNullException:“baseUri”将文件上传到Google云端硬盘时为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照这里找到的教程上传文件到Google云端硬盘:

https://developers.google.com/drive/web/quickstart/quickstart-cs



我为我的主账户创建了一个Id和Secret,并且通过该信息,我可以上传到我的两个账户(从%AppData%/ Google删除验证令牌)。然而,一位客户向我们提供了他的谷歌账户,所以我们可以尝试一切为他工作,但这对他的账户不起作用。我们可以通过Web界面上传和下载文件,没有任何问题,但它不适用于我的应用程序。



我注意到的一个区别是我的客户帐户没有任何问题,没有谷歌电子邮件地址;它是name@myclient.com,所以我使用我的工作电子邮件地址创建了一个新的Google帐户,并对其进行了测试,并且在登录到Google Drive后完美无瑕。



<最后一行出现问题:

  FilesResource.InsertMediaUpload request = service.Files.Insert(body,stream,文本/无格式); 
request.Upload();

File file = request.ResponseBody;
Console.WriteLine(文件ID:+ file.Id);

此时,request.ResponseBody为null,如果我只是运行代码file.Id抛出一个ArgumentNullException。如果我跳入调试模式并检查环境,request.Progress(来自基类的私有属性)将其Status字段设置为Failed,并且Exception字段为ArgumentNullException,并且消息参数不能为null 。参数名称:baseUri。。



有什么想法可能会出错?

更新:
创建服务对象的代码:

  //创建服务。 
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer =凭证,
ApplicationName =Drive API Sample,
});


解决方案

/ p>

将范围设置为:

static string [] Scopes = {DriveService.Scope .Drive,DriveService.Scope.DriveFile};



在我的第一次尝试中,我只使用 Drive ,而不是 DriveFile


I'm uploading files to Google Drive following the tutorial found here:

https://developers.google.com/drive/web/quickstart/quickstart-cs

I have created an Id and Secret for my main account, and with that info, I could upload to any of my two accounts (deleting the authentication token from %AppData%/Google...).

However, a client gave us his google account so we can try that everything would work for him, it didn't work for his account. We can upload and download files through the web interface without any problem, but it doesn't work with my app.

One difference I noticed is that my client's account didn't have a google e-mail address; it was name@myclient.com, so I created a new google account with my work e-mail address, tested it, and it worked flawlessly after just loggin into Google Drive.

The problem arises in the last lines:

FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain");
request.Upload();

File file = request.ResponseBody;
Console.WriteLine("File id: " + file.Id);

At that point, request.ResponseBody is null so, if I just run the code, file.Id throws an ArgumentNullException. If I break into debug mode and examine the environment instead, request.Progress (private property from a base class) has its Status field set to "Failed", and the Exception field is a ArgumentNullException, with the message "Argument can not be null. Argument name: baseUri.".

Any idea about what could be wrong?

Update: The code to create the service object:

// Create the service.
var service = new DriveService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = "Drive API Sample",
});  

解决方案

you have an OAuth Token without the correct Scope!

Set you Scope to:

static string[] Scopes = { DriveService.Scope.Drive, DriveService.Scope.DriveFile };

In my first attempts, i used only Drive and not DriveFile

这篇关于ArgumentNullException:“baseUri”将文件上传到Google云端硬盘时为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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