Reputation: 14485
I'd like to use a GoogleCredential
object (or similar) in order to create a Stackdriver logging client object (an instance of LoggingServiceV2Client
class) using some custom credentials rather than the default application credentials.
I cannot see an appropriate overload of the LoggingServiceV2Client.Create
method but the docstring for that method states:
Synchronously creates a Google.Cloud.Logging.V2.LoggingServiceV2Client, applying defaults for all unspecified settings, and creating a channel connecting to the given endpoint with application default credentials where necessary. See the example for how to use custom credentials.
which suggests it's possible somehow?
I have been unable to find a custom credentials example in the documentation anywhere. The only examples I see (eg this) read only the default application credentials from the GOOGLE_APPLICATION_CREDENTIALS
environment variable which I'd prefer to avoid
Upvotes: 1
Views: 1383
Reputation: 2836
Other solutions didn't work for me using Google.Cloud.Logging.V2 - Version: 3.4.0, because of this line:
var client = LoggingServiceV2Client.Create(channel);
In version 3.4.0 there is no constructor that takes a channel as a parameter.
So I checked google documentation : LoggingServiceV2Client Create(), and it has this small note:
To specify custom credentials or other settings, use LoggingServiceV2ClientBuilder
So here is my working code using this approach:
var credential = GoogleCredential.FromFile(jsonPath).CreateScoped(LoggingServiceV2Client.DefaultScopes);
var client = new LoggingServiceV2ClientBuilder { ChannelCredentials = credential.ToChannelCredentials() }.Build();
Upvotes: 1
Reputation: 5910
I already apreciated to @Jeffrey Rennie. In my case, I am using Cloud Text-to-Speech and I had to use following code:
Usings:
using Google.Apis.Auth.OAuth2;
using Google.Cloud.TextToSpeech.V1;
using Grpc.Auth;
Code:
// Setting up credentials
string jsonPath = @"D:\my-test-project-0078ca7c0f8c.json";
var credential = GoogleCredential.FromFile(jsonPath).CreateScoped(TextToSpeechClient.DefaultScopes);
var channel = new Grpc.Core.Channel(TextToSpeechClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());
// Instantiate a client
TextToSpeechClient client = TextToSpeechClient.Create(channel);
// Perform the Text-to-Speech request, passing the text input with the selected voice parameters and audio file type
var response = client.SynthesizeSpeech(new SynthesizeSpeechRequest
{
Input = new SynthesisInput() { Text = "My test sentence" },
Voice = new VoiceSelectionParams() { LanguageCode = "en-US", SsmlGender = SsmlVoiceGender.Male },
AudioConfig = new AudioConfig { AudioEncoding = AudioEncoding.Mp3 };
});
Installed NuGet Packages:
Google.Cloud.TextToSpeech.V1 -Pre
Google.Apis.Auth
Upvotes: 0
Reputation: 3443
It's possible, but far from obvious.
Add these two using statements to the top of your .cs:
using Google.Apis.Auth.OAuth2;
using Grpc.Auth;
Then instantiate the client like this:
var credential = GoogleCredential.FromFile(jsonPath)
.CreateScoped(LoggingServiceV2Client.DefaultScopes);
var channel = new Grpc.Core.Channel(
LoggingServiceV2Client.DefaultEndpoint.ToString(),
credential.ToChannelCredentials());
var client = LoggingServiceV2Client.Create(channel);
Upvotes: 2