01.
using
Google.Apis.Auth.OAuth2;
02.
using
Google.Apis.Drive.v3;
03.
using
Google.Apis.Drive.v3.Data;
04.
using
Google.Apis.Services;
05.
using
Google.Apis.Util.Store;
06.
using
System;
07.
using
System.Collections.Generic;
08.
using
System.IO;
09.
using
System.Linq;
10.
using
System.Text;
11.
using
System.Threading;
12.
using
System.Threading.Tasks;
13.
14.
namespace
DriveQuickstart
15.
{
16.
class
Program
17.
{
18.
19.
20.
static
string
[] Scopes = { DriveService.Scope.DriveReadonly };
21.
static
string
ApplicationName =
"Drive API .NET Quickstart"
;
22.
23.
static
void
Main(
string
[] args)
24.
{
25.
UserCredential credential;
26.
27.
using
(var stream =
28.
new
FileStream(
"client_secret.json"
, FileMode.Open, FileAccess.Read))
29.
{
30.
string
credPath = System.Environment.GetFolderPath(
31.
System.Environment.SpecialFolder.Personal);
32.
credPath = Path.Combine(credPath,
".credentials/drive-dotnet-quickstart.json"
);
33.
34.
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
35.
GoogleClientSecrets.Load(stream).Secrets,
36.
Scopes,
37.
"user"
,
38.
CancellationToken.None,
39.
new
FileDataStore(credPath,
true
)).Result;
40.
Console.WriteLine(
"Credential file saved to: "
+ credPath);
41.
}
42.
43.
44.
var service =
new
DriveService(
new
BaseClientService.Initializer()
45.
{
46.
HttpClientInitializer = credential,
47.
ApplicationName = ApplicationName,
48.
});
49.
50.
51.
FilesResource.ListRequest listRequest = service.Files.List();
52.
listRequest.PageSize = 10;
53.
listRequest.Fields =
"nextPageToken, files(id, name)"
;
54.
55.
56.
IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
57.
.Files;
58.
Console.WriteLine(
"Files:"
);
59.
if
(files !=
null
&& files.Count > 0)
60.
{
61.
foreach
(var file
in
files)
62.
{
63.
Console.WriteLine(
"{0} ({1})"
, file.Name, file.Id);
64.
}
65.
}
66.
else
67.
{
68.
Console.WriteLine(
"No files found."
);
69.
}
70.
Console.Read();
71.
72.
}
73.
}
74.
}