Authentication to Google Cloud Python API Library stopped working

1.2k Views Asked by At

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

2

There are 2 best solutions below

1
class On BEST ANSWER

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:

  • Sync Recognize is limited to 60 seconds of audio, you must use async for longer audio
  • If you haven't already, set up billing for your account
0
Jialu On

With regards to the usage problem, the issue is in fact that when you use the new google-cloud library to access ML APIs, it seems everyone authenticates to a project shared by everyone (hence it says you've used up your limit even though you've not used anything). To check and confirm this, you can call an ML API that you have not enabled by using the python client library, which will give you a result even though it shouldn't. This problem persists to other language client libraries and OS, so I suspect it's an issue with their grpc.

Because of this, to ensure consistency I always use the older googleapiclient that uses my API key. Here is an example to use the translate API:

from googleapiclient import discovery

service = discovery.build('translate', 'v2', developerKey='')

service_request = service.translations().list(q='hello world', target='zh')
result = service_request.execute()

print(result)

For the speech API, it's something along the lines of:

from googleapiclient import discovery

service = discovery.build('speech', 'v1beta1', developerKey='')

service_request = service.speech().syncrecognize()
result = service_request.execute()

print(result)

You can get the list of the discovery APIs at https://developers.google.com/api-client-library/python/apis/ with the speech one located in https://developers.google.com/resources/api-libraries/documentation/speech/v1beta1/python/latest/.

One of the other benefits of using the discovery library is that you get a lot more options compared to the current library, although often times it's a bit more of a pain to implement.