Lia*_*iam 27 python google-api
我是一名编程初学者,并且学习如何在Python中使用google API.
我有:
但是,即使我为此运行了最简单的代码,我也有错误,表示找不到凭证varialbe.
我不知道现在该怎么办.请帮忙.
这是我的代码:
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)
Run Code Online (Sandbox Code Playgroud)
我有错误:
回溯(最近一次调用最后一次):文件"/Users/YELI1/Downloads/googlecloud/sentimentanalysis/simple.py",第21行,sentiment_text(sampletxt)
文件"/Users/YELI1/Downloads/googlecloud/sentimentanalysis/simple.py",第5行,在sentiment_text client = language.LanguageServiceClient()中
在init ssl_credentials = ssl_credentials中输入文件"/usr/local/lib/python3.6/site-packages/google/cloud/gapic/language/v1/language_service_client.py",第147行
在create_stub中的文件"/usr/local/lib/python3.6/site-packages/google/gax/grpc.py",第106行
Run Code Online (Sandbox Code Playgroud)credentials = _grpc_google_auth.get_default_credentials(scopes) File"/usr/local/lib/python3.6/site-packages/google/gax/_grpc_google_auth.py",第62行,在get_default_credentials凭据中,_ = google.auth.default(scopes = scopes)
文件"/usr/local/lib/python3.6/site-packages/google/auth/_default.py",第282行,默认情况下
Run Code Online (Sandbox Code Playgroud)raise exceptions.DefaultCredentialsError(_HELP_MESSAGE) google.auth.exceptions.DefaultCredentialsError: Could not自动确定凭据.请设置GOOGLE_APPLICATION_CREDENTIALS或明确创建凭据并重新运行该应用程序.有关详细信息,请参阅 https://developers.google.com/accounts/docs/application-default-credentials.
import os
print(os.environ['GOOGLE_APPLICATION_CREDENTIALS'])
Run Code Online (Sandbox Code Playgroud)
输出回溯(最近一次调用最后一次):
Run Code Online (Sandbox Code Playgroud)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'
小智 97
如果您正在使用jupyter笔记本并想在Python代码中设置GOOGLE_APPLICATION_CREDENTIALS环境变量:
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/path/to/file.json"
Run Code Online (Sandbox Code Playgroud)
ris*_*ain 20
通过明确提及凭据并将它们传递给客户端,有一种更简单的方法可以使其工作,如下所示。
import os
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file("your-json-path-with-filename.json")
client = language.LanguageServiceClient(credentials=credentials)
Run Code Online (Sandbox Code Playgroud)
当您的代码在本地开发环境中运行时,最佳选择是使用与您的 Google 帐户关联的凭据。
安装并初始化 gcloud CLI(如果尚未安装)。
创建您的凭证文件:
gcloud auth application-default login
Run Code Online (Sandbox Code Playgroud)
将显示登录屏幕。登录后,您的凭据将存储在 ADC 使用的本地凭据文件中。然后您应该可以自动确定凭据。
我知道该帖子已得到答复,但以下是指定GOOGLE_APPLICATION_CREDENTIALS变量的更简洁的方法。
client = language.LanguageServiceClient.from_service_account_json("/path/to/file.json")
Run Code Online (Sandbox Code Playgroud)
如果您在内存中拥有凭据(例如环境变量),并且您不想专门为其创建文件:
from google.cloud import storage
from google.oauth2 import service_account
gcp_json_credentials_dict = json.loads(gcp_credentials_string)
credentials = service_account.Credentials.from_service_account_info(gcp_json_credentials_dict)
client = storage.Client(project=gcp_json_credentials_dict['project_id'], credentials=credentials)
Run Code Online (Sandbox Code Playgroud)
使用 python3.7 和 google-cloud-storage==1.35.0
| 归档时间: |
|
| 查看次数: |
38017 次 |
| 最近记录: |