forked from salrashid123/gcpsamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceAuth.cs
79 lines (65 loc) · 2.47 KB
/
ServiceAuth.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Oauth2.v2;
using Google.Apis.Oauth2.v2.Data;
using Google.Apis.Services;
using Google.Cloud.Storage.V1;
namespace Oauth2Harness
{
internal class Program
{
[STAThread]
static void Main(string[] args)
{
try
{
new Program().Run().Wait();
}
catch (AggregateException ex)
{
foreach (var err in ex.InnerExceptions)
{
Console.WriteLine("ERROR: " + err.Message);
}
}
Console.ReadKey();
}
private async Task Run()
{
/*
string CREDENTIAL_FILE_PKCS12 = "c:\\your_pkcs_cert.p12";
string serviceAccountEmail = "[email protected]";
var certificate = new X509Certificate2(CREDENTIAL_FILE_PKCS12, "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { Oauth2Service.Scope.UserinfoEmail }
}.FromCertificate(certificate));
*/
// a) for google apis:
//string CREDENTIAL_FILE_JSON = "C:\\your_json_cert.json";
//Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", CREDENTIAL_FILE_JSON);
//GoogleCredential credential = await GoogleCredential.GetApplicationDefaultAsync();
if (credential.IsCreateScopedRequired)
credential = credential.CreateScoped(new string[] { Oauth2Service.Scope.UserinfoEmail });
var service = new Oauth2Service(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Oauth2 Sample",
});
Console.WriteLine(service.Userinfo.Get().Execute().Email);
// b) For Google Cloud APIs:
var client = StorageClient.Create();
foreach (var obj in client.ListObjects("your_project", ""))
{
Console.WriteLine(obj.Name);
}
}
}
}