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!

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
tmighty
  • 10,734
  • 21
  • 104
  • 218
  • 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. – Pac0 Mar 10 '21 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. – tmighty Mar 10 '21 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 – Panagiotis Kanavos Mar 10 '21 at 12:08
  • The actual idea behind an environment variable is that it's provided by the machine, available for users/app to read or edit. If you don't want this behavior, then you don't want to use environment variables – Pac0 Mar 10 '21 at 12:09
  • 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](https://cloud.google.com/docs/authentication/production#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. – Panagiotis Kanavos Mar 10 '21 at 12:11
  • 1
    The docs show how to [load the credentials in code](https://cloud.google.com/docs/authentication/production#passing_code) using `GoogleCredential.FromFile(jsonPath)` – Panagiotis Kanavos Mar 10 '21 at 12:15

1 Answers1

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;
tmighty
  • 10,734
  • 21
  • 104
  • 218