Roberto Jarzembowski
Roberto Jarzembowski

Reputation: 97

Application Default Credentials not available - localhost project

I'm trying to make a call to Google Speech API using the library method (I don't want to make a direct call to an endpoint) and I get this error:

The Application Default Credentials are not available.
They are available if running in Google Compute Engine.
Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS
must be defined pointing to a file defining the credentials.

I have already downloaded the JSON credentials file from my service account and defined the GOOGLE_APPLICATION_CREDENTIALS environment variable pointing to it.

I have also created this GCLOUD_PROJECT variable which is set to the "project_id" field in my JSON file.

I am not providing any extra authentication when using the library method - but this has worked in another local environment which probably had some other configuration that I am missing.

 var speech = SpeechClient.Create();
            var response = speech.Recognize(new RecognitionConfig()
            {
                Encoding = RecognitionConfig.Types.AudioEncoding.Linear16,

                LanguageCode = languageCode,
            }, RecognitionAudio.FromFile(file));

The authentication file is of type service account, if that helps.

"type": "service_account",

Upvotes: 3

Views: 4453

Answers (2)

umutcanturan
umutcanturan

Reputation: 78

Setting the environment variable worked for me. Use the code:

System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", <<path>>);

You need to use path of json file that you get from Google Api page.

Upvotes: 4

A.Queue
A.Queue

Reputation: 1572

I found that your code is similar to code in this repo dotnet-docs-samples So I tried to replicate what you describe in a GCP Compute Engine instance.

$ curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
$ sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
$ sudo apt-get update
$ sudo apt-get install dotnet-sdk-2.0.2
$ git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git
$ cd dotnet-docs-samples/speech/api/QuickStart/


$ dotnet run 

At this point the environment was set, except for the variables. So predictably dotnet run command failed with an error.

$ GOOGLE_APPLICATION_CREDENTIALS=/home/qalexander/test-case-qalexander-2084b2876481.json
$ GCLOUD_PROJECT=test-case-qalexander
$ dotnet run

The variables were not exported and execution failed.

$ export GOOGLE_APPLICATION_CREDENTIALS=/home/qalexander/test-case-qalexander-2084b2876481.json
$ export GCLOUD_PROJECT=test-case-qalexander
$ dotnet run

Variables were exported and execution went successfully:

Check your that your variables are actually exported into environment. For Windows there are following commands here] in section 3 subsection i:

For this powershell session.

PS > $env:GOOGLE_APPLICATION_CREDENTIALS = "$env:USERPROFILE\Downloads\your-project-id-dea9fa230eae3.json"

For all processes created after this command.

PS > [Environment]::SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "$env:USERPROFILE\Downloads\your-project-id-dea9fa230eae3.json", "User")

In case this doesn’t help, please check your environment variables:

$ export #bash

Or for Windows:

PS> Get-ChildItem Env:

Upvotes: 0

Related Questions