【问题标题】:Why do I get credential read in error on google slide API?为什么我在 google slide API 上读取凭据时出错?
【发布时间】:2019-03-13 22:31:12
【问题描述】:

我从以下位置下载了 gsuite 开发者 python 代码:

(https://github.com/gsuitedevs/python-samples)

然后我启用了 api 访问并下载了 credentials.json 文件并在以下位置运行了 quickstart.py:

(python-samples-master/slides/quickstart) 它工作并输出了

The presentation contains 5 slides:
- Slide #1 contains 4 elements.
- Slide #2 contains 11 elements.
- Slide #3 contains 9 elements.
- Slide #4 contains 5 elements.
- Slide #5 contains 12 elements.

所以它奏效了。然后我尝试在以下位置运行 test_sn-ps.py:

(python-samples-master/slides/sn-ps)

我得到一个错误

======================================================================
ERROR: setUpClass (__main__.SnippetsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "~/anaconda3/lib/python3.7/site-packages/oauth2client/client.py", line 1228, in _implicit_credentials_from_files
    credentials_filename)
  File "~/anaconda3/lib/python3.7/site-packages/oauth2client/client.py", line 1397, in _get_application_default_credential_from_file
    AUTHORIZED_USER + "' or '" + SERVICE_ACCOUNT + "' values)")
oauth2client.client.ApplicationDefaultCredentialsError: 'type' field should be defined (and have one of the 'authorized_user' or 'service_account' values)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test_snippets.py", line 32, in setUpClass
    super(SnippetsTest, cls).setUpClass()
  File "~/Desktop/python-samples-master/slides/snippets/base_test.py", line 27, in setUpClass
    cls.credentials = cls.create_credentials()
  File "~/Desktop/python-samples-master/slides/snippets/base_test.py", line 44, in create_credentials
    credentials = GoogleCredentials.get_application_default()
  File "~/anaconda3/lib/python3.7/site-packages/oauth2client/client.py", line 1271, in get_application_default
    return GoogleCredentials._get_implicit_credentials()
  File "~/anaconda3/lib/python3.7/site-packages/oauth2client/client.py", line 1256, in _get_implicit_credentials
    credentials = checker()
  File "~/anaconda3/lib/python3.7/site-packages/oauth2client/client.py", line 1231, in _implicit_credentials_from_files
    extra_help, error)
  File "~/anaconda3/lib/python3.7/site-packages/oauth2client/client.py", line 1429, in _raise_exception_for_reading_json
    credential_file + extra_help + ': ' + str(error))
oauth2client.client.ApplicationDefaultCredentialsError: An error was encountered while reading json file: ~/Documents/credentials/credentials.json (pointed to by GOOGLE_APPLICATION_CREDENTIALS environment variable): 'type' field should be defined (and have one of the 'authorized_user' or 'service_account' values)

我肯定有一个 GOOGLE_APPLICATION_CREDENTIALS 指向成功运行 quickstart.py 的相同凭据。

我还需要什么或者我需要更改一些代码来加载凭据数据吗?

似乎 GoogleCredentials.get_application_default() 调用是错误的

    def create_credentials(cls):
        credentials = GoogleCredentials.get_application_default()
        scope = [
            'https://www.googleapis.com/auth/drive',
        ]
        return credentials.create_scoped(scope)

【问题讨论】:

    标签: google-api google-cloud-platform google-oauth google-slides-api


    【解决方案1】:

    指向环境变量GOOGLE_APPLICATION_CREDENTIALS的文件不是有效的服务帐号json文件。

    打开您的服务帐户 json 文件。文件的开头应该类似于:

    {
      "type": "service_account",
      "project_id": "development-123456",
      "private_key_id": "19c38bac6560abcdef01234567ac4da7991cbaad",
      "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANB
    

    【讨论】:

    • 好的,是的,我刚得到一个新的服务帐户 json 文件,格式看起来像您发布的带有一些其他信息的格式。
    猜你喜欢
    • 2018-04-16
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 1970-01-01
    相关资源
    最近更新 更多