Last active
August 23, 2019 14:52
-
-
Save Timopheym/c8ec7cde514b18c899290eb92da85b05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Security.Cryptography.X509Certificates; | |
using Google.Apis.Auth.OAuth2; | |
using Google.Apis.Drive.v3; | |
using Google.Apis.Drive.v3.Data; | |
using Google.Apis.Services; | |
namespace Google.Apis.Samples.PlusServiceAccount | |
{ | |
/// <summary> | |
/// This sample demonstrates the simplest use case for a Service Account service. | |
/// The certificate needs to be downloaded from the Google API Console | |
/// <see cref="https://console.developers.google.com/"> | |
/// "Create another client ID..." -> "Service Account" -> Download the certificate, | |
/// rename it as "key.p12" and add it to the project. Don't forget to change the Build action | |
/// to "Content" and the Copy to Output Directory to "Copy if newer". | |
/// </summary> | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
Console.WriteLine("Plus API - Service Account"); | |
Console.WriteLine("=========================="); | |
String serviceAccountEmail = "drive-cloner@swift-cursor-176011.iam.gserviceaccount.com"; | |
var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable); | |
ServiceAccountCredential credential = new ServiceAccountCredential( | |
new ServiceAccountCredential.Initializer(serviceAccountEmail) | |
{ | |
Scopes = new[] { DriveService.Scope.Drive } | |
}.FromCertificate(certificate)); | |
// Create the service. | |
var service = new DriveService(new BaseClientService.Initializer() | |
{ | |
HttpClientInitializer = credential, | |
ApplicationName = "Plus API Sample", | |
}); | |
String fileId = "1FBlwZSwRAWXypHl39u0Whn3Ry3ttNj-BnMEHCISzqsA"; | |
File file = new File(); | |
service.Files.Copy(file, fileId).Execute(); | |
Console.WriteLine("==List of files start=="); | |
FileList fileList2 = service.Files.List().Execute(); | |
foreach (File f in fileList2.Files) | |
{ | |
Console.WriteLine(f.Name); | |
} | |
Console.WriteLine("==List of files end=="); | |
Console.WriteLine("Press any key to continue..."); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment