pythongoogle-cloud-visiongoogle-cloud-speechgoogle-cloud-pythongcloud-python

Authentication to Google Cloud Python API Library stopped working


I have problems with the authentication in the Python Library of Google Cloud API. At first it worked for some days without problem, but suddenly the API calls are not showing up in the API Overview of the Google CloudPlatform.

I created a service account and stored the json file locally. Then I set the environment variable GCLOUD_PROJECT to the project ID and GOOGLE_APPLICATION_CREDENTIALS to the path of the json file.

from google.cloud import speech
client = speech.Client()
print(client._credentials.service_account_email)

prints the correct service account email.

The following code transcribes the audio_file successfully, but the Dashboard for my Google Cloud project doesn't show anything for the activated Speech API Graph.

import io
with io.open(audio_file, 'rb') as f:
    audio = client.sample(f.read(), source_uri=None, sample_rate=48000, encoding=speech.encoding.Encoding.FLAC)

alternatives = audio.sync_recognize(language_code='de-DE')

At some point the code also ran in some errors, regarding the usage limit. I guess due to the unsuccessful authentication, the free/limited option is used somehow.

I also tried the alternative option for authentication by installing the Google Cloud SDK and gcloud auth application-default login, but without success.

I have no idea where to start troubleshooting the problem. Any help is appreciated!

(My system is running Windows 7 with Anaconda)

EDIT: The error count (Fehler) is increasing with calls to the API. How can I get detailed information about the error?!

gclouderror


Solution

  • Make sure you are using an absolute path when setting the GOOGLE_APPLICATION_CREDENTIALS environment variable. Also, you might want to try inspecting the access token using OAuth2 tokeninfo and make sure it has "scope": "https://www.googleapis.com/auth/cloud-platform" in its response.

    Sometimes you will get different error information if you initialize the client with GRPC enabled:

    0.24.0: speech_client = speech.Client(_use_grpc=True)

    0.23.0: speech_client = speech.Client(use_gax=True)

    Usually it's an encoding issue, can you try with the sample audio or try generating LINEAR16 samples using something like the Unix rec tool:

    rec --channels=1 --bits=16 --rate=44100 audio.wav trim 0 5
    

    ...

    with io.open(speech_file, 'rb') as audio_file:
        content = audio_file.read()
        audio_sample = speech_client.sample(
            content,
            source_uri=None,
            encoding='LINEAR16',
            sample_rate=44100)
    

    Other notes: