【发布时间】:2016-03-15 23:34:44
【问题描述】:
我正在尝试连接到我自己的个人 Google Drive 帐户并收集文件名列表。
我做了什么:
- 安装了所有需要的 NuGet 包
- 在 Google Developers Console 的 API 管理器中添加了 Google Drive
- 设置Service Account并下载P12密钥进行身份验证
-
编写了如下代码调用API:
string EMAIL = "myprojectname@appspot.gserviceaccount.com"; string[] SCOPES = { DriveService.Scope.Drive }; StringBuilder sb = new StringBuilder(); X509Certificate2 certificate = new X509Certificate2(@"c:\\DriveProject.p12", "notasecret", X509KeyStorageFlags.Exportable); ServiceAccountCredential credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(EMAIL) { Scopes = SCOPES }.FromCertificate(certificate) ); DriveService service = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = credential }); FilesResource.ListRequest listRequest = service.Files.List(); IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute().Files; if (files != null && files.Count > 0) foreach (var file in files) sb.AppendLine(file.Name);
此代码似乎运行良好。问题是我只返回了一个文件,它被命名为“Getting started.pdf”,我不知道它是从哪里来的。我认为问题显然是我的个人 Google Drive 帐户没有连接到此代码。如何获得此调用以从我的个人 Google Drive 帐户返回文件?
我能找到的唯一帮助是试图让您在您的界面中访问任何最终用户的 Google Drive 帐户。我的情况与此不同。我只想在后台连接到我的 Google Drive 帐户。
【问题讨论】:
标签: .net google-api google-drive-api google-api-dotnet-client service-accounts