2017-08-04 2 views
3

私はプログラミングの初心者で、PythonでGoogle APIを使用する方法を学びます。Google APIを使用するには、PythonプロジェクトでGOOGLE_APPLICATION_CREDENTIALSを設定してください。

私が持っている:

  1. は、Googleクラウド上のプロジェクトを作成し、私が使用するAPI、自然言語のAPIを有効に。
  2. は資格を作成し、資格JSONファイルをダウンロードし、私はこのコマンドを実行しているターミナルではapikey.JSON
  3. として
  4. それを保存します。export GOOGLE_APPLICATION_CREDENTIALS = apikey.JSON、エラーがポップません。

しかし、私はこのためのコードの中で最も単純なものを実行したとしても、資格情報のバリアルが見つからないというエラーがあります。

私は今何をすべきか分かりません。親切にお手伝いください。

これは私のコードです:

from google.cloud import language 

def sentiment_text(text): 

    client = language.LanguageServiceClient() 

    sentiment = client.analyze_sentiment(text).document_sentiment 

    print('Score: {}'.format(sentiment.score)) 
    print('Magnitude: {}'.format(sentiment.magnitude)) 

sampletxt='Python is great' 

sentiment_text(sampletxt) 

そして、私はエラーがあります:あなたはjupyterノートブックに取り組んとしたいしている場合

Traceback (most recent call last): File "/Users/YELI1/Downloads/googlecloud/sentimentanalysis/simple.py", line 21, in sentiment_text(sampletxt)

File "/Users/YELI1/Downloads/googlecloud/sentimentanalysis/simple.py", line 5, in sentiment_text client = language.LanguageServiceClient()

File "/usr/local/lib/python3.6/site-packages/google/cloud/gapic/language/v1/language_service_client.py", line 147, in init ssl_credentials=ssl_credentials)

File "/usr/local/lib/python3.6/site-packages/google/gax/grpc.py", line 106, in create_stub

credentials = _grpc_google_auth.get_default_credentials(scopes) File 

"/usr/local/lib/python3.6/site-packages/google/gax/_grpc_google_auth.py", line 62, in get_default_credentials credentials, _ = google.auth.default(scopes=scopes)

File "/usr/local/lib/python3.6/site-packages/google/auth/_default.py", line 282, in default

raise exceptions.DefaultCredentialsError(_HELP_MESSAGE) google.auth.exceptions.DefaultCredentialsError: Could not 

automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credential and re-run the application. For more information, please see https://developers.google.com/accounts/docs/application-default-credentials .

import os 
print(os.environ['GOOGLE_APPLICATION_CREDENTIALS']) 

Output Traceback (most recent call last):

File "/Users/YELI1/Downloads/googlecloud/sentimentanalysis/simple‌​.py", line 2, in <module> 
print(os.environ['GOOGLE_APPLICATION_CREDENTIALS']) 
    File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework‌​/Versions/3.6/lib/py‌​thon3.6/os.py", line 669, in getitem 
raise KeyError(key) from None KeyError: 'GOOGLE_APPLICATION_CREDENTIALS' 
+0

'print(os.environ ['GOOGLE_APPLICATION_CREDENTIALS'])'が表示されます。 'インポートOS 印刷(はos.environ [' GOOGLE_APPLICATION_CREDENTIALS ']') 出力 トレースバック(最新の呼び出しの最後)を実行している – stovfl

+0

コード: ファイル "/Users/YELI1/Downloads/googlecloud/sentimentanalysis/simple.pyを"、 2行目、 print(os.environ ['GOOGLE_APPLICATION_CREDENTIALS']) ファイル "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/os .py "、行669、__getitem__内 KeyError(キー)をNoneから KeyError: 'GOOGLE_APPLICATION_CREDENTIALS' – Liam

+0

このように書式を設定する方法はわかりませんが、混乱していますが、そのようなキーはないようです「GOOGLE_APPLICATION_CREDENTIALS」 @stovfl – Liam

答えて

6

を環境変数GOOGLE_APPLICATION_CREDENTIALSを設定するPythonコードで可能:

import os 
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/path/to/file.json" 
関連する問題