-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathGCEAuth.cs
61 lines (52 loc) · 1.85 KB
/
GCEAuth.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
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Storage.v1.Data;
using Google.Cloud.Storage.V1;
using Google.Apis.Auth.OAuth2;
namespace AuthHarness
{
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);
}
}
}
private async Task Run()
{
var googleCredential = await GoogleCredential.GetApplicationDefaultAsync().ConfigureAwait(false);
ICredential credential = googleCredential.UnderlyingCredential;
ComputeCredential computeCredential = credential as ComputeCredential;
if (await ComputeCredential.IsRunningOnComputeEngine().ConfigureAwait(false)) {
var projectID = Google.Api.Gax.Platform.Instance().ProjectId;
var storage = StorageClient.Create();
var buckets = storage.ListBuckets(projectID);
Console.WriteLine("Buckets:");
foreach (var bucket in buckets)
{
Console.WriteLine(bucket.Name);
}
// TODO: get metadata values directly without using a raw httpclient
// the following is limited to a few compute-centric values
// var instance_host = Google.Api.Gax.Platform.Instance().GceDetails;
// Console.WriteLine(instance_host);
} else {
Console.WriteLine("not on gce");
return;
}
}
}
}