1

In my application, I use the Google Cloud Translation service.

On my developer machine, I have the Google credentials environment variable installed.

However, when deployed, my application throws the following error:

The Application Default Credentials are not available.

The Environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined.

They are avaiable if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.

I thought that the credentials are automatically compiled into my application.

What might be the error here?

Thank you!

6
  • if it's an environment variable, then it's not part of your code. You must set the environment variable on the client machine as part of your deployment / installation process, or find another way. Commented Mar 10, 2021 at 12:02
  • @Pac0 Does a compiled application on the customer machine require that the environment variable is set on the customer's machine? Sounds pretty harsh to me. Commented Mar 10, 2021 at 12:03
  • The Google client expects its credentials to come from this environment variable, or any other configuration mechanism it supports. The error message is pretty clear and explains what you need to do, and even links to the docs Commented Mar 10, 2021 at 12:08
  • 1
    You can't connect to any cloud service without authentication. The linked docs explain how this works and how to manually specify credentials in Passing Credentials Manually. Using environment variables is convenient and makes deployment on Docker and Kubernetes a lot easier - if you want to modify settings, you let the orchestrator change the environment variables of the containers as needed. Commented Mar 10, 2021 at 12:11
  • 1
    The docs show how to load the credentials in code using GoogleCredential.FromFile(jsonPath) Commented Mar 10, 2021 at 12:15

1 Answer 1

1

The JSON file needs to be compiled as an Embedded Ressource.

Here is the solution: http://www.hzhang.org/Umbraco/blog/use-google-cloud-translation-with-c/

Use Google Cloud Translation with C#

Follow the guide to set up an account.

Download the private key as JSON and save it under folder Assets (e.g. API Project-xxxxxxx.json).

IMPORTANT: set its Build Action as Embedded resource.

Install NuGet package Google.Cloud.Translation.V2.

Create TranslationClient

TranslationClient _tc; GoogleCredential gc = GoogleCredential.FromStream(Utility.GetStreamFromResourceFile("API Project-xxxxxxx.json", GetType())); _tc = TranslationClient.Create(gc); 

You can traslate like the following example that translates "Text to be translated" from German to English:

 string sTranslatedText = _tc.TranslateText("Text to be translated", "en", "de").TranslatedText; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.