Google cloud speech api throws 403 when trying to use it

I am using python with google cloud speech api. I have done all the steps in How to use google speech recognition api in python? "on ubuntu and on windows as well as when I try to run a simple script here - https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/speech/api/speech_rest.py "

I get the following error: <HttpError 403 when requesting https://speech.googleapis.com/$discovery/rest?version=v1beta1 returned "Google Cloud Speech API has not been used in project google.com:cloudsdktool before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/speech.googleapis.com/overview?project=google.com:cloudsdktool then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.">

What is strange is that I do not have a project called "cloudsdktool"

I run "gcloud init" and linked the json file that I received when I created the service account key using the gcloud command auth activate-service-account -key-file = jsonfile, I tried to create a google credential environment variable in linux, and still i get the same massage

+5
source share
1 answer

So, I found two ways to fix this problem:

1 - if you use google cloud sdk and cloud speech is in beta, you need to run "gcloud beta init" instead of "gcloud init" and then provide a json file

2 - if you do not want to use cloud sdk from google, you can transfer the json file directly to the python application

Here are the methods for doing this:

 from oauth2client.client import GoogleCredentials GoogleCredentials.from_stream('path/to/your/json') 

then you simply create a scope on credits and authorization or, if you use grpc (streaming), you pass it to the header, as in the example.

Here are the modified scripts for grpc:

 def make_channel(host, port): """Creates an SSL channel with auth credentials from the environment.""" # In order to make an https call, use an ssl channel with defaults ssl_channel = implementations.ssl_channel_credentials(None, None, None) # Grab application default credentials from the environment creds = GoogleCredentials.from_stream('path/to/your/json').create_scoped([SPEECH_SCOPE]) # Add a plugin to inject the creds into the header auth_header = ( 'Authorization', 'Bearer ' + creds.get_access_token().access_token) auth_plugin = implementations.metadata_call_credentials( lambda _, cb: cb([auth_header], None), name='google_creds') # compose the two together for both ssl and google auth composite_channel = implementations.composite_channel_credentials( ssl_channel, auth_plugin) return implementations.secure_channel(host, port, composite_channel) 
+3
source

All Articles