Reputation: 223
I am trying publish to google PubSub from my .NET Core App. I have already created the PubSub and a Topic in Google PubSub. I downloaded the private key json file and included in my project. I am able to read the file and create the credential but I don't see a way to pass the credential to Google's PubSub Publisher client. I have looked at the GitHub Post but unfortunately I am not seeing a property named DefaultEndPoint.
Running my code generates an error which is looking for environment variable for credential. I do not want to set the environment variable for now and if there is no alternative I will try that.
Following is my code and let me know what I am doing wrong of is something that got changed recently.
var credential = GoogleCredential.FromFile("app-services.json"); PublisherClient publisher = PublisherClient.CreateAsync(topicName).Result;
Upvotes: 3
Views: 6545
Reputation: 196
Creating a PublisherServiceApiClient without environment variables:
var credential = GoogleCredential.FromJson(@"{""your_credentials_json_itself"":""here""}");
var channel = new Grpc.Core.Channel(PublisherServiceApiClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());
var publisher = PublisherServiceApiClient.Create(channel);
Upvotes: 2
Reputation: 1705
Agreed that this isn't at obvious as it should be.
using Grpc.Auth;
var credential = GoogleCredential.FromFile("app-services.json");
var createSettings = new PublisherClient.ClientCreationSettings(
credentials: credential.ToChannelCredentials());
var publisher = await PublisherClient.CreateAsync(topicName,
clientCreationSettings: createSettings);
Upvotes: 10