LichKing LichKing - 11 months ago 85
Linux Question

Google cloud speech api throwing 403 when trying to use it

I'm using python with google cloud speech api I did all the steps in "How to use google speech recognition api in python?" on ubuntu and on windows as well and when I trying to run the simple script from here - ""

I get the next error:

<HttpError 403 when requesting$discovery/rest?version=v1beta1 returned "Google Cloud Speech API has not been used in project before or it is disabled. Enable it by visiting then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.">

what is weird is that I don't have project by the name "cloudsdktool"

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

Answer Source

So I found two ways to fix that problem:

1 - if using google cloud sdk and the cloud speech is in beta version you need to run 'gcloud beta init' instead of 'gcloud init' and then provide the json file

2 - if you don't want to use the cloud sdk from google you can pass the json file straight in python app

here are the methods for this:

from oauth2client.service_account import client


then you just create scope on the creds and authorizing or if using grpc(streaming) you pass it to the header just like in the example.

here are the changed script for the 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 = client._get_application_default_credential_from_file('path/to/your/json').create_scoped([SPEECH_SCOPE])
    # Add a plugin to inject the creds into the header
    auth_header = (
        'Bearer ' + creds.get_access_token().access_token)
    auth_plugin = implementations.metadata_call_credentials(
        lambda _, cb: cb([auth_header], None),

    # 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)