在 Python 项目中设置 GOOGLE_APPLICATION_CREDENTIALS 以使用 Google API [英] Set GOOGLE_APPLICATION_CREDENTIALS in Python project to use Google API

查看:76
本文介绍了在 Python 项目中设置 GOOGLE_APPLICATION_CREDENTIALS 以使用 Google API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名编程初学者,我正在尝试学习如何通过 Python 使用 google API.

I am a programming beginner and thing I'm trying to learn how to use google API with Python.

我有:

  1. 在 Google Cloud 上创建了一个项目并启用了我想要使用的 API,Natural Language API.
  2. 创建凭证并下载凭证JSON文件,将其保存为apikey.JSON
  3. 在终端中,我运行了这个命令:export GOOGLE_APPLICATION_CREDENTIALS=apikey.JSON,没有弹出错误.
  1. created a project on Google Cloud and enabled the API that I want to use, Natural Language API.
  2. created a credential and downloaded the credential JSON file, saved it as apikey.JSON
  3. In the Terminal I have ran this command: export GOOGLE_APPLICATION_CREDENTIALS=apikey.JSON, no error popped.

然而,即使我为此运行了最简单的代码,我也有错误提示找不到凭证变量.

However, even when I ran the simplest of codes for this, I have errors which says the credential variable is not found.

我不知道现在该怎么办.请帮助.

I am not sure what to do now. Please kindly help.

这是我的代码:

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)

我有错误:

> Traceback (most recent call last):   File
> "/Users/YELI1/Downloads/googlecloud/sentimentanalysis/simple.py", line
> 21, in <module>
>     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']) 
   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'

推荐答案

如果您正在使用 jupyter notebook 并想在 Python 代码中设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量:

If you're working on a jupyter notebook and want to set GOOGLE_APPLICATION_CREDENTIALS environment variable in Python code :

import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/path/to/file.json"

这篇关于在 Python 项目中设置 GOOGLE_APPLICATION_CREDENTIALS 以使用 Google API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆