private ChannelCredentials CreatedScopedChannelCredentials(params string[] scopes) { if (_computeCredentials != null) { return(_computeCredentials.ToChannelCredentials()); } else if (_googleCredentials != null) { return(_googleCredentials.CreateScoped(scopes).ToChannelCredentials()); } else if (_serviceCredentials != null) { return(_serviceCredentials.ToChannelCredentials()); } else { throw new InvalidCredentialException("Credentials were not set"); } }
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); } }
/// <summary> /// /// <para>BTracingServiceGC: Parametered Constructor for Managed Service by Google</para> /// /// <para>Parameters:</para> /// <para><paramref name="_ProjectID"/> GC Project ID</para> /// <para><paramref name="_ProgramUniqueID"/> Program Unique ID</para> /// <para><paramref name="_ErrorMessageAction"/> Error messages will be pushed to this action</para> /// /// </summary> public BTracingServiceGC( string _ProjectID, string _ProgramUniqueID, Action <string> _ErrorMessageAction = null) { ProgramUniqueID = _ProgramUniqueID; ErrorMessageAction = _ErrorMessageAction; try { string ApplicationCredentials = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS"); string ApplicationCredentialsPlain = Environment.GetEnvironmentVariable("GOOGLE_PLAIN_CREDENTIALS"); if (ApplicationCredentials == null && ApplicationCredentialsPlain == null) { _ErrorMessageAction?.Invoke("BTracingServiceGC->Constructor: GOOGLE_APPLICATION_CREDENTIALS (or GOOGLE_PLAIN_CREDENTIALS) environment variable is not defined."); bInitializationSucceed = false; } else { var Scopes = new List <string>(); foreach (var Scope in TraceServiceClient.DefaultScopes) { if (!Scopes.Contains(Scope)) { Scopes.Add(Scope); } } if (ApplicationCredentials == null) { if (!BUtility.HexDecode(out ApplicationCredentialsPlain, ApplicationCredentialsPlain, _ErrorMessageAction)) { throw new Exception("Hex decode operation for application credentials plain has failed."); } Credential = GoogleCredential.FromJson(ApplicationCredentialsPlain) .CreateScoped( Scopes.ToArray()) .UnderlyingCredential as ServiceAccountCredential; } else { using (var Stream = new FileStream(ApplicationCredentials, FileMode.Open, FileAccess.Read)) { Credential = GoogleCredential.FromStream(Stream) .CreateScoped( Scopes.ToArray()) .UnderlyingCredential as ServiceAccountCredential; } } if (Credential != null) { Channel = new Grpc.Core.Channel( TraceServiceClient.DefaultEndpoint.ToString(), Credential.ToChannelCredentials()); } if (Channel != null) { TraceClient = TraceServiceClient.Create(Channel); ProjectName = new Google.Api.Gax.ResourceNames.ProjectName(_ProjectID); bInitializationSucceed = TraceClient != null; UploadTimer = new Timer(1_000); UploadTimer.Elapsed += OnTimedEvent; UploadTimer.AutoReset = true; UploadTimer.Enabled = true; } else { bInitializationSucceed = false; } } } catch (Exception e) { _ErrorMessageAction?.Invoke("BTracingServiceGC->Constructor: " + e.Message + ", Trace: " + e.StackTrace); bInitializationSucceed = false; } }
/// <summary> /// /// <para>BPubSubServiceGC: Parametered Constructor for Managed Service by Google</para> /// /// <para>Parameters:</para> /// <para><paramref name="_ProjectID"/> GC Project ID</para> /// <para><paramref name="_ErrorMessageAction"/> Error messages will be pushed to this action</para> /// /// </summary> public BPubSubServiceGC( string _ProjectID, Action <string> _ErrorMessageAction = null) { ProjectID = _ProjectID; try { string ApplicationCredentials = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS"); string ApplicationCredentialsPlain = Environment.GetEnvironmentVariable("GOOGLE_PLAIN_CREDENTIALS"); if (ApplicationCredentials == null && ApplicationCredentialsPlain == null) { _ErrorMessageAction?.Invoke("BPubSubServiceGC->Constructor: GOOGLE_APPLICATION_CREDENTIALS (or GOOGLE_PLAIN_CREDENTIALS) environment variable is not defined."); bInitializationSucceed = false; } else { var PublishScopes = new List <string>(); var SubscribeScopes = new List <string>(); foreach (var Scope in PublisherServiceApiClient.DefaultScopes) { if (!PublishScopes.Contains(Scope)) { PublishScopes.Add(Scope); } } foreach (var Scope in SubscriberServiceApiClient.DefaultScopes) { if (!SubscribeScopes.Contains(Scope)) { SubscribeScopes.Add(Scope); } } if (ApplicationCredentials == null) { if (!BUtility.HexDecode(out ApplicationCredentialsPlain, ApplicationCredentialsPlain, _ErrorMessageAction)) { throw new Exception("Hex decode operation for application credentials plain has failed."); } PublishCredential = GoogleCredential.FromJson(ApplicationCredentialsPlain) .CreateScoped( PublishScopes.ToArray()) .UnderlyingCredential as ServiceAccountCredential; SubscribeCredential = GoogleCredential.FromJson(ApplicationCredentialsPlain) .CreateScoped( SubscribeScopes.ToArray()) .UnderlyingCredential as ServiceAccountCredential; } else { using (var Stream = new FileStream(ApplicationCredentials, FileMode.Open, FileAccess.Read)) { PublishCredential = GoogleCredential.FromStream(Stream) .CreateScoped( PublishScopes.ToArray()) .UnderlyingCredential as ServiceAccountCredential; SubscribeCredential = GoogleCredential.FromStream(Stream) .CreateScoped( SubscribeScopes.ToArray()) .UnderlyingCredential as ServiceAccountCredential; } } if (PublishCredential != null && SubscribeCredential != null) { PublishChannel = new Channel( PublisherServiceApiClient.DefaultEndpoint.ToString(), PublishCredential.ToChannelCredentials()); SubscribeChannel = new Channel( SubscriberServiceApiClient.DefaultEndpoint.ToString(), SubscribeCredential.ToChannelCredentials()); bInitializationSucceed = true; } else { bInitializationSucceed = false; } } } catch (Exception e) { _ErrorMessageAction?.Invoke("BPubSubServiceGC->Constructor: " + e.Message + ", Trace: " + e.StackTrace); bInitializationSucceed = false; } }