2016-08-10 73 views
5

我在google雲語音api中使用python我在ubuntu和windows上執行了「How to use google speech recognition api in python?」中的所有步驟,以及當我嘗試運行簡單腳本時在這裏 - 「https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/speech/api/speech_rest.py谷歌雲語音api在嘗試使用時投擲403

我得到了一個錯誤: <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.">

是什麼奇怪的是,我不要被名字有項目「cloudsdktool」

我跑「gcloud初始化」,並將我在創建服務帳戶密鑰時獲得的json文件鏈接到「gcloud auth a ctivate服務帳戶--key文件= jsonfile」命令, 我試圖在Linux創建谷歌憑據環境變量,我仍然得到同樣的按摩

+0

相關討論https://github.com/GoogleCloudPlatform/google-cloud-ruby/issues/706。看起來你需要一個JSON文件的身份驗證。 –

+1

[Google Vision API文本檢測Python示例使用項目可能重複:「google.com:cloudsdktool」,而不是我自己的項目](http://stackoverflow.com/questions/38048320/google-vision-api-text-detection -python-example-uses-project-google-comclouds) –

回答

3

於是我發現了兩個方法來解決這個問題:

1 - 如果使用google雲sdk並且雲語音處於beta版本,則需要運行'gcloud beta init'而不是'gcloud init',然後提供json文件

2 - 如果您不想使用從谷歌雲sdk你可以直接在python應用程序傳遞json文件

這裏是方法fo R此:

from oauth2client.client import GoogleCredentials 

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

那麼你只要創建vim的信任狀範圍和授權,或者使用GRPC(流),你將它傳遞給頭就像在例子。

這兩個是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

謝謝,它還解決了其他庫的測試版,在我的情況下語言APi:http://stackoverflow.com/questions/38048320/google-vision- API-文本檢測中的python-例如用途項目,谷歌,comclouds – Neurus