1
votes

I'm currently trying to access the cloud API and I am receiving 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 gone onto my google cloud platform account, enabled billing, created a service account, downloaded a JSON key and set GOOGLE_APPLICATION_CREDENTIALS in my environment variables and have checked that it's pointing to the correct JSON key. It is pointing to my API key 'C:\Users\jade.wilson\source\repos\CloudApi\CloudApi\Services\Keys\CloudAPIKey.json'

This is the code:

   var credential = GoogleCredential.GetApplicationDefault();
   var channel = new Grpc.Core.Channel(SpeechClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());

   var client = SpeechClient.Create(channel);

   response = await client.RecognizeAsync(new RecognitionConfig()
            {
                Encoding = RecognitionConfig.Types.AudioEncoding.Linear16,
                SampleRateHertz = 16000,
                LanguageCode = "en",
            }, RecognitionAudio.FromStream(audio));

It is not getting past the first line. Is there something else I am missing?

1
What's the error you're getting? - TheChubbyPanda
@TheChubbyPanda 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. - Jade
Ah sorry, missed that in the question, I'm gonna recommend an edit. - TheChubbyPanda
Make sure you do not have a typo in something. How are you executing your program - you have specified a path based from your home directory so normally you need to be logged in as that user to have rights to the file. - John Hanley
Could you tell us how you've set GOOGLE_APPLICATION_CREDENTIALS exactly? If that environment variable is set, I'd expect you to get a different error if the file is wrong etc. (I could be wrong on that front, mind you.) If you log Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS") on start-up, do you see the value you expect? (My guess is that you've set the environment variable for one user, but not the user running the code.) Also note that just using var client = SpeechClient.Create(); is the simple way to use the default endpoint and default credentials. - Jon Skeet

1 Answers

1
votes

If you are using Visual Studio, just close and reopen it.

It seems that if you create/alter environment variables while Visual Studio is open, the programs that you'll run/debug won't be able to see them. You have to exit Visual Studio and reenter (happened to me: VS 2019 ver. 16.7.2, Win 10 ver. 2004 - OS Build 19041.450)