Is It Possible To Manually Supply a GoogleCredential To SpeechClient (In .NET API)?
Asked Answered
C

3

6

All of the documentation for SpeechClient that I've found involves either running a command line after downloading the SDK, or awkwardly setting up a "GOOGLE_APPLICATION_CREDENTIALS" environment variable to point to a local credential file.

I hate the environment variable approach, and instead want a solution that loads a shared, source-controlled dev account file from the application root. Something like this:

var credential = GoogleCredential.FromStream(/*load shared file from app root*/);
var client = SpeechClient.Create(/*I wish I could pass credential in here*/);

Is there a way to do this so that I don't have to rely on the environment variable?

Crossbill answered 17/3, 2017 at 19:43 Comment(1)
As well as Jeffrey's answer, see googlecloudplatform.github.io/google-cloud-dotnet/docs/faq.htmlChromolithography
A
15

Yes, by converting the GoogleCredential into a ChannelCredentials, and using that to initialize a Channel, which you then wrap in a SpeechClient:

using Grpc.Auth;

//...

GoogleCredential googleCredential;
using (Stream m = new FileStream(credentialsFilePath, FileMode.Open))
    googleCredential = GoogleCredential.FromStream(m);
var channel = new Grpc.Core.Channel(SpeechClient.DefaultEndpoint.Host,
    googleCredential.ToChannelCredentials());
var speech = SpeechClient.Create(channel);

Update 2018-02-02 https://cloud.google.com/docs/authentication/production now shows all they possible ways to authenticate to a Google Cloud Service, including a sample like this one.

Atomize answered 20/3, 2017 at 21:36 Comment(5)
Where is ToChannelCredentials() coming from? I'm apparently missing a reference or on a different version.Crossbill
I figured it out. I had to reference the namespace Grpc.Auth to get the extension method.Crossbill
You also need to install the Grpc.Auth Nuget package.Kantianism
I get an error when trying this: System.TypeLoadException: Could not resolve type with token 0100003e (from typeref, class/assembly System.Runtime.Loader.AssemblyLoadContext, System.Runtime.Loader, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a) any ideas?Delainedelainey
If I use the method given on the examples, I have a "Deadline Exceeded" error. If I set manually the env variable in the code with the same json files, it works, any idea why?Hitherward
D
1

In latest version SpeechClient.Create does not have any parameters.

Now it is possible to do it using SpeechClientBuilder:

var client = new SpeechClientBuilder { ChannelCredentials = credentials.ToChannelCredentials() }.Build();
Dort answered 17/2, 2021 at 12:57 Comment(0)
W
-1
'Install-Package Google.Cloud.Speech.V1 -Version 3.0.0
Imports Google.Cloud.Speech.V1

'Dim sCredentialsPath As String = "C:\google_keys\deft.json"
'Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", sCredentialsPath)
'Dim oSpeechClient As SpeechClient = Google.Cloud.Speech.V1.SpeechClient.Create()

Dim oSpeechSettings As New SpeechSettings()
Dim oSpeechClient = New SpeechClientBuilder With {
    .CredentialsPath = sCredentialsPath
}.Build()
Dim oRecognitionAudio As RecognitionAudio = RecognitionAudio.FromFile("C:\Temp\test.weba")

Dim oRecognitionConfig As New RecognitionConfig
'oRecognitionConfig.SampleRateHertz = 44100
oRecognitionConfig.LanguageCode = LanguageCodes.English.UnitedStates '"en-US"
oRecognitionConfig.Encoding = RecognitionConfig.Types.AudioEncoding.WebmOpus

Dim oRecognizeResponse As RecognizeResponse = oSpeechClient.Recognize(oRecognitionConfig, oRecognitionAudio)
Dim sRet As String = ""

For Each oResult As Google.Cloud.Speech.V1.SpeechRecognitionResult In oRecognizeResponse.Results
    For Each oAlternative As Google.Cloud.Speech.V1.SpeechRecognitionAlternative In oResult.Alternatives
        sRet += oAlternative.Transcript
    Next
Next
Wasteful answered 17/10, 2022 at 16:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.