Exemple #1
0
        private async Task Run()
        {
// 1. no basic auth, with usercredentials
// need to set export http_proxy=http://localhost:3128 for Pubsub
// need to set ProxySupportedHttpClientFactory for GCS and oauth2

//  auth Y
//  gcs Y
//  pubsub Y


// 1638323879.659    693 192.168.9.1 TCP_TUNNEL/200 45147 CONNECT storage.googleapis.com:443 - HIER_DIRECT/172.253.63.128 -
// 1638323879.659    884 192.168.9.1 TCP_TUNNEL/200 7349 CONNECT oauth2.googleapis.com:443 - HIER_DIRECT/142.251.45.10 -
// 1638323879.659    372 192.168.9.1 TCP_TUNNEL/200 7878 CONNECT pubsub.googleapis.com:443 - HIER_DIRECT/172.217.13.234 -


// 2. no basic auth, with service account credentials
// export GOOGLE_APPLICATION_CREDENTIALS=/path/to/svc_account.json
//  auth N
//  gcs N
//  pubsub Y

// 3. no basicauth, with ServiceAccountCredential
            //var stream = new FileStream("/path/to/svc_account.json", FileMode.Open, FileAccess.Read);
            //ServiceAccountCredential sacredential = ServiceAccountCredential.FromServiceAccountData(stream);
            GoogleCredential credential = await GoogleCredential.GetApplicationDefaultAsync();

            credential = credential.CreateWithHttpClientFactory(new ProxySupportedHttpClientFactory());

            StorageService service = new StorageService(new BaseClientService.Initializer
            {
                HttpClientInitializer = credential,
                ApplicationName       = StorageClientImpl.ApplicationName,
                HttpClientFactory     = new ProxySupportedHttpClientFactory(),
            });
            var client = new StorageClientImpl(service, null);

            foreach (var b in client.ListBuckets(projectID))
            {
                Console.WriteLine(b.Name);
            }

            PublisherServiceApiClient publisher = PublisherServiceApiClient.Create();
            ProjectName projectName             = ProjectName.FromProject(projectID);

            foreach (Topic t in publisher.ListTopics(projectName))
            {
                Console.WriteLine(t.Name);
            }
        }
        private async Task Run()
        {
            string CREDENTIAL_FILE_PKCS12       = "/path/to/your/cert/file.p12";
            string serviceAccountEmail          = "*****@*****.**";
            var    certificate                  = new X509Certificate2(CREDENTIAL_FILE_PKCS12, "notasecret", X509KeyStorageFlags.Exportable);
            ServiceAccountCredential credential = new ServiceAccountCredential(
                new ServiceAccountCredential.Initializer(serviceAccountEmail)
            {
                //Scopes = new[] { StorageService.Scope.DevstorageReadOnly, PublisherClient.DefaultScopes },
                Scopes            = PublisherClient.DefaultScopes.Append(StorageService.Scope.DevstorageReadOnly),
                HttpClientFactory = new ProxySupportedHttpClientFactory()
            }.FromCertificate(certificate));


            //GoogleCredential credential = await GoogleCredential.GetApplicationDefaultAsync();
            //StorageService service = StorageClient.Create(credential);

            StorageService service = new StorageService(new BaseClientService.Initializer
            {
                HttpClientInitializer = credential,
                ApplicationName       = StorageClientImpl.ApplicationName,
                HttpClientFactory     = new ProxySupportedHttpClientFactory(),
            });
            var client = new StorageClientImpl(service, null);

            foreach (var b in client.ListBuckets(projectID))
            {
                Console.WriteLine(b.Name);
            }

            ChannelCredentials channelCredentials = credential.ToChannelCredentials();
            Channel            channel            = new Channel(PublisherClient.DefaultEndpoint.ToString(), channelCredentials);
            PublisherSettings  ps        = new PublisherSettings();
            PublisherClient    publisher = PublisherClient.Create(channel, ps);

            foreach (Topic t in publisher.ListTopics(new ProjectName(projectID)))
            {
                Console.WriteLine(t.Name);
            }
        }