Google traslate api-PermissionError:[Errno 1]不允许的操作:

时间:2019-10-19 22:43:51

标签: python google-api google-translate

我使用anaconda导航器,并且开始学习如何使用Google的apis

我已经按照在线说明开始使用api https://cloud.google.com/translate/docs/reference/libraries

我陷入的第一部分是在这个阶段

  

通过设置环境变量> GOOGLE_APPLICATION_CREDENTIALS为您的应用程序代码提供身份验证凭据。将[PATH]替换为包含您的服务帐户密钥的JSON文件的文件路径,并将[FILE_NAME]替换为文件名。此变量仅适用于当前的Shell会话,因此,如果您打开一个新会话,请再次设置该变量。

我按终端中的要求插入了数据,但是当我回显GOOGLE_APPLICATION_CREDENTIALS时却找不到任何内容。

因此,我求助于我在网上找到的第二个解决方案

导入操作系统 os.environ [“ GOOGLE_APPLICATION_CREDENTIALS”] =“ / path / to / json / file”

这样,程序将重新定位GOOGLE_APPLICATION_CREDENTIALS的位置

现在的问题是,当我运行以下代码时

# 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']))

我收到以下错误


---------------------------------------------------------------------------
PermissionError                           Traceback (most recent call last)
<ipython-input-2-a721e1abc1b1> in <module>
      3 
      4 # Instantiates a client
----> 5 translate_client = translate.Client()
      6 
      7 # The text to translate

~/opt/anaconda3/lib/python3.7/site-packages/google/cloud/translate_v2/client.py in __init__(self, target_language, credentials, _http, client_info, client_options)
     80     ):
     81         self.target_language = target_language
---> 82         super(Client, self).__init__(credentials=credentials, _http=_http)
     83 
     84         kw_args = {"client_info": client_info}

~/opt/anaconda3/lib/python3.7/site-packages/google/cloud/client.py in __init__(self, credentials, _http)
    130             raise ValueError(_GOOGLE_AUTH_CREDENTIALS_HELP)
    131         if credentials is None and _http is None:
--> 132             credentials, _ = google.auth.default()
    133         self._credentials = google.auth.credentials.with_scopes_if_required(
    134             credentials, self.SCOPE

~/opt/anaconda3/lib/python3.7/site-packages/google/auth/_default.py in default(scopes, request)
    303 
    304     for checker in checkers:
--> 305         credentials, project_id = checker()
    306         if credentials is not None:
    307             credentials = with_scopes_if_required(credentials, scopes)

~/opt/anaconda3/lib/python3.7/site-packages/google/auth/_default.py in _get_explicit_environ_credentials()
    163     if explicit_file is not None:
    164         credentials, project_id = _load_credentials_from_file(
--> 165             os.environ[environment_vars.CREDENTIALS])
    166 
    167         return credentials, project_id

~/opt/anaconda3/lib/python3.7/site-packages/google/auth/_default.py in _load_credentials_from_file(filename)
     89             'File {} was not found.'.format(filename))
     90 
---> 91     with io.open(filename, 'r') as file_obj:
     92         try:
     93             info = json.load(file_obj)

PermissionError: [Errno 1] Operation not permitted: '/Users/path/togooglecredentials.json

我不明白为什么程序不允许返回操作

0 个答案:

没有答案
相关问题