3
votes

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;

3

3 Answers

8
votes

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);
2
votes

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);
1
votes