【发布时间】:2017-04-01 16:56:30
【问题描述】:
我可以通过 Drive API 检索校验和吗?
This answer 通过文档中的“试用”功能进行操作,该功能的界面很糟糕。
我如何通过 .NET (C#) Google Drive API 来做到这一点? 或者,我如何使用自己的发布/获取请求来做到这一点?
【问题讨论】:
标签: c# .net google-drive-api drive
我可以通过 Drive API 检索校验和吗?
This answer 通过文档中的“试用”功能进行操作,该功能的界面很糟糕。
我如何通过 .NET (C#) Google Drive API 来做到这一点? 或者,我如何使用自己的发布/获取请求来做到这一点?
【问题讨论】:
标签: c# .net google-drive-api drive
试试.NET Quickstart,因为它是一种基本的列表文件方法。
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Drive.v3.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DriveQuickstart
{
class Program
{
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/drive-dotnet-quickstart.json
static string[] Scopes = { DriveService.Scope.DriveReadonly };
static string ApplicationName = "Drive API .NET Quickstart";
static void Main(string[] args)
{
UserCredential credential;
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Drive API service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
FilesResource.ListRequest listRequest = service.Files.List();
listRequest.PageSize = 10;
listRequest.Fields = "nextPageToken, files(id, name)";
// List files.
IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
.Files;
Console.WriteLine("Files:");
if (files != null && files.Count > 0)
{
foreach (var file in files)
{
Console.WriteLine("{0} ({1})", file.Name, file.Id);
}
}
else
{
Console.WriteLine("No files found.");
}
Console.Read();
}
}
}
}
}
编辑这一行:
listRequest.Fields = "nextPageToken, files(id, name)";
收件人:
listRequest.Fields = "nextPageToken, files(id, name, md5Checksum)";
上面链接的解决方案是正确的,Try it 功能将帮助您熟悉每种方法以及响应和请求是如何形成的。
希望这会有所帮助。
【讨论】: