设置环境变量问题
Setting Environmental variable issue
我的 google api 测试出现身份验证错误,设置环境变量的代码单独工作,但是当与 google 测试代码结合使用时授权失败。我还能够通过 运行 shell 中的 EXPORT
命令使测试代码正常工作。为了方便起见,我希望能够在我的脚本中设置身份验证。
#This program will test the google speech api
#while setting the environmental variable for the
#authentication .json file and then authenticating
import os
# Imports the Google Cloud client library
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
# My code sets the authentication env variable
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = str('home/kyle/Security/test.json')
# Instantiates a client
client = language.LanguageServiceClient()
# The text to analyze
text = u'Hello, world!'
document = types.Document(
content=text,
type=enums.Document.Type.PLAIN_TEXT)
# Detects the sentiment of the text
sentiment = client.analyze_sentiment(document=document).document_sentiment
print('Text: {}'.format(text))
print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude))
错误:
File "/home/kyle/python projects/googleSpeech/test/venv/lib/python3.5/site-packages/google/auth/_default.py", line 142, in _get_explicit_environ_credentials
os.environ[environment_vars.CREDENTIALS])
File "/home/kyle/python projects/googleSpeech/test/venv/lib/python3.5/site-packages/google/auth/_default.py", line 67, in _load_credentials_from_file
'File {} was not found.'.format(filename))
google.auth.exceptions.DefaultCredentialsError: File home/kyle/Security/test.json was not found.
EDIT 现在导出 json 文件不起作用我 运行 此代码用于测试读取 .json 文件
fd = open(‘/home/kyle/Security/test.json’)
dat = fd.read()
fd.close()
结果:
SyntaxError: Non-ASCII character '\xe2' in file testRead.py on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
仔细观察,我认为可能是这一行:
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] =
str('home/kyle/Security/test.json')
在home/kyle/....前面加一个/,这样就是/home/kyle/...
该问题要求我从 api 控制台重新生成一个新的 json 密钥,并获得项目所有者的许可,现在可以正常工作,无需更改代码
我的 google api 测试出现身份验证错误,设置环境变量的代码单独工作,但是当与 google 测试代码结合使用时授权失败。我还能够通过 运行 shell 中的 EXPORT
命令使测试代码正常工作。为了方便起见,我希望能够在我的脚本中设置身份验证。
#This program will test the google speech api
#while setting the environmental variable for the
#authentication .json file and then authenticating
import os
# Imports the Google Cloud client library
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
# My code sets the authentication env variable
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = str('home/kyle/Security/test.json')
# Instantiates a client
client = language.LanguageServiceClient()
# The text to analyze
text = u'Hello, world!'
document = types.Document(
content=text,
type=enums.Document.Type.PLAIN_TEXT)
# Detects the sentiment of the text
sentiment = client.analyze_sentiment(document=document).document_sentiment
print('Text: {}'.format(text))
print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude))
错误:
File "/home/kyle/python projects/googleSpeech/test/venv/lib/python3.5/site-packages/google/auth/_default.py", line 142, in _get_explicit_environ_credentials
os.environ[environment_vars.CREDENTIALS])
File "/home/kyle/python projects/googleSpeech/test/venv/lib/python3.5/site-packages/google/auth/_default.py", line 67, in _load_credentials_from_file
'File {} was not found.'.format(filename))
google.auth.exceptions.DefaultCredentialsError: File home/kyle/Security/test.json was not found.
EDIT 现在导出 json 文件不起作用我 运行 此代码用于测试读取 .json 文件
fd = open(‘/home/kyle/Security/test.json’)
dat = fd.read()
fd.close()
结果:
SyntaxError: Non-ASCII character '\xe2' in file testRead.py on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
仔细观察,我认为可能是这一行:
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = str('home/kyle/Security/test.json')
在home/kyle/....前面加一个/,这样就是/home/kyle/...
该问题要求我从 api 控制台重新生成一个新的 json 密钥,并获得项目所有者的许可,现在可以正常工作,无需更改代码