0

I'm currently working on using the Google Cloud Platform Speech-to-Text Api. I've followed the prompts and read some of the possible errors and troubleshooting information on the developer site. But I can't seem to make the env variable needed to access the auth credentials.

The name of the variable is GOOGLE_APPLICATION_CREDENTIALS. I have tried the following:

  1. Setting up the variable using the command prompt: set GOOGLE_APPLICATION_CREDENTIALS=[path]
  2. Setting up the variable using the anaconda power shell: $env:GOOGLE_APPLICATION_CREDENTIALS=[path]
  3. Including the path in PATH variables on the environment variables native option on Windows 10
  4. Including the environment variables on the same native option state above.
  5. Setting up the variable inside Sublime3 using the following:
def explicit():
    from google.cloud import storage

    # Explicitly use service account credentials by specifying the private key
    # file.
    storage_client = storage.Client.from_service_account_json(
        'path')

    # Make an authenticated API request
    buckets = list(storage_client.list_buckets())
    print(buckets)

The code I am trying to run is the following:

import io
import os

# Imports the Google Cloud client library
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types


# Instantiates a client
client = speech.SpeechClient()

# The name of the audio file to transcribe
file_name = os.path.join(
    os.path.dirname(__file__),
    'resources',
    'audio.raw')

# Loads the audio into memory
with io.open(file_name, 'rb') as audio_file:
    content = audio_file.read()
    audio = types.RecognitionAudio(content=content)

config = types.RecognitionConfig(
    encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
    sample_rate_hertz=16000,
    language_code='en-US')

# Detects speech in the audio file
response = client.recognize(config, audio)

for result in response.results:
    print('Transcript: {}'.format(result.alternatives[0].transcript))

I have read other answers on setting up and verifying environment variables, but no solution has worked.

I can also see the environment in the env folder in the anaconda shell prompt. Does anyone have any ideas as to why the script cannot find the file?

The error message is:

line 317, in default
    raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application.
EdAlex
  • 25
  • 8
  • Relevant [avoiding-defaultcredentialserror](https://stackoverflow.com/questions/41385341/avoiding-defaultcredentialserror-when-using-google-bigquery-api) – stovfl Oct 30 '19 at 16:22
  • I'm not sure if this is directly a Sublime question or not; do you get different results if you manually run your program from a command prompt versus trying to run it in Sublime? – OdatNurd Oct 30 '19 at 18:30
  • Hi @OdatNurd, you are right I do get the same errors - I'll edit the question now. – EdAlex Oct 30 '19 at 22:47
  • Have you tried explicitly printing out the environment variable inside your python script? This will at least validate whether the script can 'see' the env variable - `print(os.environ['GOOGLE_APPLICATION_CREDENTIALS'])` – elembie Oct 30 '19 at 22:56
  • @elembie yes and it gives me a KeyError – EdAlex Oct 31 '19 at 00:06
  • And have you validated that you can print the environment variable from the command line where you're running the script? `echo $env:GOOGLE_APPLICATION_CREDENTIALS` – elembie Oct 31 '19 at 00:14
  • There's also a known [bug](https://github.com/googleapis/google-cloud-python/issues/1883) - not sure if this is relevant. – elembie Oct 31 '19 at 00:20
  • Yeah, it returns the correct file - and I'm working through that thread to see if their suggestions fix mine, but so far no luck – EdAlex Oct 31 '19 at 00:32

1 Answers1

0

Hi I was also facing the same issue but resolved it later. So the reason the path is not getting set is probably because the file is blocked by the system. Just go to the location where the file is and right click on the file -> properties. there you will see an option called unblock. click on it and that should resolve your problem.

randomGeek4
  • 151
  • 1
  • 2
  • 7