google.auth.exceptions.DefaultCredentialsError:
google.auth.exceptions.DefaultCredentialsError:
因此,我完全按照 Google 翻译 API 的文档执行相同的过程。
里面提供了下面的代码。
# Imports the Google Cloud client library
from google.cloud import translate
# Instantiates a client
translate_client = translate.Client()
# The text to translate
text = u'Hello, world!'
# The target language
target = 'ru'
# Translates some text into Russian
translation = translate_client.translate(
text,
target_language=target)
print(u'Text: {}'.format(text))
print(u'Translation: {}'.format(translation['translatedText']))
现在当我 运行 返回时出现以下错误:
Traceback (most recent call last): File "test.py", line 5, in <module>
translate_client = translate.Client() File "C:\ProgramData\Anaconda3\lib\site-packages\google\cloud\translate_v2\client.py", line 65, in __init__
super(Client, self).__init__(credentials=credentials, _http=_http) File "C:\ProgramData\Anaconda3\lib\site-packages\google\cloud\client.py", line 129, in __init__
credentials, _ = google.auth.default() File "C:\ProgramData\Anaconda3\lib\site-packages\google\auth\_default.py", line 294, in default
credentials, project_id = checker() File "C:\ProgramData\Anaconda3\lib\site-packages\google\auth\_default.py", line 165, in _get_explicit_environ_credentials
os.environ[environment_vars.CREDENTIALS]) File "C:\ProgramData\Anaconda3\lib\site-packages\google\auth\_default.py", line 89, in _load_credentials_from_file
'File {} was not found.'.format(filename)) google.auth.exceptions.DefaultCredentialsError: File D:\Summer Projects\Translate\social media analysis-2a59d94ba22d.json was not found.
D:\Summer Projects\Translate\social media analysis-2a59d94ba22d.json
是我放置由 Google Cloud 为该项目提供的密钥的路径。
我该如何解决?
您需要为GOOGLE_APPLICATION_CREDENTIALS
设置环境变量
您可以通过添加以下行将其添加到您的代码中:
credential_path = "D:\Summer Projects\Translate\social media analysis-2a59d94ba22d.json"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path
完整的解决方案:
# Imports the Google Cloud client library
import os
from google.cloud import translate
credential_path = "D:\Summer Projects\Translate\social media analysis-2a59d94ba22d.json"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path
# Instantiates a client
translate_client = translate.Client()
# The text to translate
text = u'Hello, world!'
# The target language
target = 'ru'
# Translates some text into Russian
translation = translate_client.translate(
text,
target_language=target)
print(u'Text: {}'.format(text))
print(u'Translation: {}'.format(translation['translatedText']))
我现在正在处理愿景API。
我的解决方案取决于上面的答案。
但是 google 中的示例代码与之前的代码有点不同。
我的示例代码是
def run_quickstart():
import os
import io
from google.cloud import vision
from google.cloud.vision import types
...
# I made the modification here
credential_path = "PATH"
# for example
# credential_path = "projectname-xxxxxxxx.json"
# WRONG code
# credential_path = "~/Downloads/projectname-xxxxxxxx.json"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path
需要注意的是我把JSON
文件放到了和.py
同一个文件夹里
所以 PATH
实际上并不是指向 JSON 文件
的真实路径
它只会转到 JSON 文件的名称,例如 projectname-xxxxxxxx.json
因此,我完全按照 Google 翻译 API 的文档执行相同的过程。
里面提供了下面的代码。
# Imports the Google Cloud client library
from google.cloud import translate
# Instantiates a client
translate_client = translate.Client()
# The text to translate
text = u'Hello, world!'
# The target language
target = 'ru'
# Translates some text into Russian
translation = translate_client.translate(
text,
target_language=target)
print(u'Text: {}'.format(text))
print(u'Translation: {}'.format(translation['translatedText']))
现在当我 运行 返回时出现以下错误:
Traceback (most recent call last): File "test.py", line 5, in <module>
translate_client = translate.Client() File "C:\ProgramData\Anaconda3\lib\site-packages\google\cloud\translate_v2\client.py", line 65, in __init__
super(Client, self).__init__(credentials=credentials, _http=_http) File "C:\ProgramData\Anaconda3\lib\site-packages\google\cloud\client.py", line 129, in __init__
credentials, _ = google.auth.default() File "C:\ProgramData\Anaconda3\lib\site-packages\google\auth\_default.py", line 294, in default
credentials, project_id = checker() File "C:\ProgramData\Anaconda3\lib\site-packages\google\auth\_default.py", line 165, in _get_explicit_environ_credentials
os.environ[environment_vars.CREDENTIALS]) File "C:\ProgramData\Anaconda3\lib\site-packages\google\auth\_default.py", line 89, in _load_credentials_from_file
'File {} was not found.'.format(filename)) google.auth.exceptions.DefaultCredentialsError: File D:\Summer Projects\Translate\social media analysis-2a59d94ba22d.json was not found.
D:\Summer Projects\Translate\social media analysis-2a59d94ba22d.json
是我放置由 Google Cloud 为该项目提供的密钥的路径。
我该如何解决?
您需要为GOOGLE_APPLICATION_CREDENTIALS
您可以通过添加以下行将其添加到您的代码中:
credential_path = "D:\Summer Projects\Translate\social media analysis-2a59d94ba22d.json"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path
完整的解决方案:
# Imports the Google Cloud client library
import os
from google.cloud import translate
credential_path = "D:\Summer Projects\Translate\social media analysis-2a59d94ba22d.json"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path
# Instantiates a client
translate_client = translate.Client()
# The text to translate
text = u'Hello, world!'
# The target language
target = 'ru'
# Translates some text into Russian
translation = translate_client.translate(
text,
target_language=target)
print(u'Text: {}'.format(text))
print(u'Translation: {}'.format(translation['translatedText']))
我现在正在处理愿景API。 我的解决方案取决于上面的答案。
但是 google 中的示例代码与之前的代码有点不同。
我的示例代码是
def run_quickstart():
import os
import io
from google.cloud import vision
from google.cloud.vision import types
...
# I made the modification here
credential_path = "PATH"
# for example
# credential_path = "projectname-xxxxxxxx.json"
# WRONG code
# credential_path = "~/Downloads/projectname-xxxxxxxx.json"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path
需要注意的是我把JSON
文件放到了和.py
同一个文件夹里
所以 PATH
实际上并不是指向 JSON 文件
的真实路径
它只会转到 JSON 文件的名称,例如 projectname-xxxxxxxx.json