.netcredentialsgoogle-cloud-translate

.NET: The Application Default Credentials are not available


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!


Solution

  • 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;