Search code examples
c#google-apispeech-to-textgoogle-speech-to-text-api

How to use a regional endpoint in Google Speech-to-Text C# API?


I would like to use the European endpoint eu-speech.googleapis.com for the Google Cloud speech recognition C# API. An example is given for Python at https://cloud.google.com/speech-to-text/docs/endpoints#speech-sync-recognize-python

My credentials are stored in a json file. When setting up the SpeechClient as follows, an invalid authentication exception is thrown:

using Google.Apis.Auth.OAuth2;
using Google.Cloud.Speech.V1;
...
SpeechClientBuilder scb = new SpeechClientBuilder
{
    Credential = GoogleCredential.FromFile("mycredentials.json")
};
scb.Endpoint = "eu-speech.googleapis.com"; // Without this line everything works.
SpeechClient speechClient = scb.Build();
speechClient.Recognize(...);

Exception: Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential.

How can I set the endpoint correctly? Do I break my credentials when setting the endpoint? Should the endpoint be included within the json file?

Thanks for your help!


Solution

  • Apparently, the C# API has an issue. Fortunately, Jon Skeet provided two workarounds:

    var client = new SpeechClientBuilder
    {
        GoogleCredential = GoogleCredential.FromFile("mycredentials.json"),
        Endpoint = "eu-speech.googleapis.com"
    }.Build();
    

    Alternatively:

    var client = new SpeechClientBuilder
    {
        CredentialsPath = "mycredentials.json",
        Endpoint = "eu-speech.googleapis.com"
    }.Build();
    

    See issue at Regional Endpoint cannot be set in SpeechClientBuilder in Google.Cloud.Speech.V1 version 3.0.0 #8798