如何使用OAuth在C#中创建新的Google云端硬盘服务 [英] How do you create a new Google Drive Service in C# using OAuth

查看:56
本文介绍了如何使用OAuth在C#中创建新的Google云端硬盘服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的命令行应用程序,该应用程序会将文件上传到Google云端硬盘.我将按照此处提供的步骤进行操作: https://developers.google. com/drive/web/quickstart/quickstart-cs

I'm trying to write a simple command line application that will upload a file to Google Drive. I'm following along with the steps provided here: https://developers.google.com/drive/web/quickstart/quickstart-cs

他们提供的示例代码无法编译,特别是以下行:

The sample code they provide doesn't compile, specifically the line:

var service = new DriveService(new BaseClientService.Initializer()
            {
                Authenticator = auth
            });

这将返回错误:

"'Google.Apis.Services.BaseClientService.Initalizer'不包含'Authenticator'的定义"

"'Google.Apis.Services.BaseClientService.Initalizer' does not contain a definition for 'Authenticator'"

显然,API已经更改,但是我找不到执行此操作的新方法.

obviously the APIs have changed but I can't find the new way to do this.

推荐答案

首先,您缺少验证码.另外,如果您使用的apis.drive.v2无法正常工作,则您所引用的教程将使用较旧版本的库.此版本适用于较新的库:My Tutorial Google Drive api C#(还有一个winform示例项目)

First off you are missing the autentication code. Also the tutorial you are refering to uses an older version of the libary if you are using apis.drive.v2 that wont work. This one works with the newer libarys: My Tutorial Google Drive api C# (there is also a winform sample project)

但是这里有一些如何使它工作的示例:

But heres some Examples of how you can get it working:

UserCredential credential; 
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
          new ClientSecrets { ClientId = "YourClientId", ClientSecret = "YourClientSecret" },
          new[] { DriveService.Scope.Drive,
            DriveService.Scope.DriveFile },
          "user",
          CancellationToken.None,
          new FileDataStore("Drive.Auth.Store")).Result; }

上面的代码将为您提供用户凭证.它会弹出一个新的浏览器窗口,要求用户自动启动您的应用程序. 我的教程在这里

The code above will get you the User Credentials. It pops up a new browser window asking the user to autorise your app. My Tutorial here

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

上面的代码使您可以访问驱动器服务.看看我们如何将其从Oauth呼叫中获得的凭证发送给它?然后要上传它,只需调用service.files.insert

The code above allow you to access the Drive Service. See how we send it the credential that we got from the Oauth call? Then to upload its a matter of calling service.files.insert

// File's metadata.
File body = new File();
body.Title = "NewDirectory2";
body.Description = "Test Directory";
body.MimeType = "application/vnd.google-apps.folder";
body.Parents = new List<ParentReference>() { new ParentReference() { Id = "root" } };
try
   {
   FilesResource.InsertRequest request = service.Files.Insert(body);
   request.Execute(); 
  }
 catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
}

这篇关于如何使用OAuth在C#中创建新的Google云端硬盘服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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