2016-08-10 12 views
5

Używam Pythona z Google Cloud Speech API zrobiłem wszystkie kroki w "How to use google speech recognition api in python?" na ubuntu i na oknach, a także, gdy próbuję uruchomić prosty skrypt z tu - "https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/speech/api/speech_rest.py"Google cloud mowy api rzucając 403 podczas próby użycia go

dostanę następny błąd: <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.">

co jest dziwne jest to, że nie mam projekt o nazwie "cloudsdktool"

biegnę "gcloud init" i połączyłem plik json, który otrzymałem, gdy utworzyłem klucz konta usługi z "gcloud auth a ctivate-service-konto --key-file = komenda jsonfile” Próbowałem w linux stworzenia google poświadczeń zmienna i nadal uzyskać ten sam masaż

+0

pokrewne dyskusja https://github.com/GoogleCloudPlatform/google-cloud-ruby/issues/706. Wygląda na to, że potrzebujesz auth z plikiem json. –

+1

Możliwy duplikat wykrycia tekstu interfejsu API Google Vision Python używa projektu: "google.com:cloudsdktool", a nie mojego własnego projektu] (http://stackoverflow.com/questions/38048320/google-vision-api-text-detection -python-example-uses-project-google-comclouds) –

Odpowiedz

3

Więc znalazłem dwa sposoby, aby rozwiązać ten problem:

1 - jeśli korzystasz z usługi Google Cloud SDK, a chmura mowy jest w wersji beta, musisz uruchomić "gcloud beta init" zamiast "gcloud init", a następnie dostarczyć plik json

2 - jeśli nie chcesz użyj pliku SDK w chmurze z google Możesz przekazać plik json prosto w python app

oto metody R to:

from oauth2client.client import GoogleCredentials 

GoogleCredentials.from_stream('path/to/your/json') 

potem po prostu utworzyć zakres na creds i autoryzacja lub w przypadku korzystania grpc (strumieniowe) przekazać go do nagłówka tak jak w przykładzie.

oto zmieniony skrypt dla 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) 
+0

Dziękuję, rozwiązuje to również dla innych bibliotek w wersji beta, w moim przypadku język APi: http://stackoverflow.com/questions/38048320/google-vision- api-text-detection-python-example-uses-project-google-comclouds – Neurus