【问题标题】:Why am I getting 503 service unavailable on my sample google api code (python, admin, directory_v1)?为什么我的示例 google api 代码(python、admin、directory_v1)上出现 503 服务不可用?
【发布时间】:2020-02-01 16:47:52
【问题描述】:

我有一个管理员 Google 帐户,并且有权为我的大学创建自定义域用户 (example@stu.najah.edu)。 我想编写一个 python 脚本来自动化这个任务,所以我使用了谷歌的 API。 我跟着这个教程(https://github.com/googleapis/google-api-python-client/blob/master/docs/oauth-server.md) 并做了一切。但仍然从 python 中得到以下异常:

/Users/osamaabuomar/projects/.virtualenvs/gsuite-api-demo/bin/python /Users/osamaabuomar/projects/gsuite-api-demo/quickstart.py
Getting the first 10 users in the domain
Traceback (most recent call last):
  File "/Users/osamaabuomar/projects/gsuite-api-demo/quickstart.py", line 32, in <module>
    main()
  File "/Users/osamaabuomar/projects/gsuite-api-demo/quickstart.py", line 19, in main
    results = service.users().list(customer='my_customer', maxResults=10, orderBy='email').execute()
  File "/Users/osamaabuomar/projects/.virtualenvs/gsuite-api-demo/lib/python3.7/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/Users/osamaabuomar/projects/.virtualenvs/gsuite-api-demo/lib/python3.7/site-packages/googleapiclient/http.py", line 856, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 503 when requesting https://www.googleapis.com/admin/directory/v1/users?customer=my_customer&maxResults=10&orderBy=email&alt=json returned "Service unavailable. Please try again">

Process finished with exit code 1

这是我的python代码

from __future__ import print_function
from google.oauth2 import service_account
import googleapiclient.discovery


def main():
    SCOPES = ['https://www.googleapis.com/auth/admin.directory.user',
              'https://www.googleapis.com/auth/admin.directory.customer']

    SERVICE_ACCOUNT_FILE = './quickstart-1570011757324-0609ceb3ce31.json'

    credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

    service = googleapiclient.discovery.build('admin', 'directory_v1', credentials=credentials)

    # Call the Admin SDK Directory API
    print('Getting the first 10 users in the domain')
    results = service.users().list(customer='my_customer', maxResults=10, orderBy='email').execute()
    users = results.get('users', [])

    if not users:
        print('No users in the domain.')
    else:
        print('Users:')
        for user in users:
            print(u'{0} ({1})'.format(user['primaryEmail'],
                                      user['name']['fullName']))


if __name__ == '__main__':
    main()

【问题讨论】:

标签: python google-api google-oauth google-admin-sdk


【解决方案1】:

问题是我错过了设置委托,正如DalmTo 提到的那样。 所以这是我完整的工作代码:

from __future__ import print_function
from google.oauth2 import service_account
import googleapiclient.discovery

SCOPES = ['https://www.googleapis.com/auth/admin.directory.user', ]

SERVICE_ACCOUNT_FILE = './quickstart-1570011757324-2bfbc3d902b9.json'


def main():
    credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)
    delegated_credentials = credentials.with_subject('admin@najah.edu')

    service = googleapiclient.discovery.build('admin', 'directory_v1', credentials=delegated_credentials)

    # Call the Admin SDK Directory API
    print('Getting the first 10 users in the domain')
    results = service.users().list(customer='my_customer', maxResults=10, orderBy='email').execute()
    users = results.get('users', [])

    if not users:
        print('No users in the domain.')
    else:
        print('Users:')
        for user in users:
            print(u'{0} ({1})'.format(user['primaryEmail'],
                                      user['name']['fullName']))


if __name__ == '__main__':
    main()

注意credentials.with_subject('admin@domain.com') 部分。

【讨论】:

  • 在 PHP 中有类似的问题。我还想指出,在我按照 Google 的指示启用 API 客户端访问之前,尝试添加“主题”声明会给我一个错误:support.google.com/a/answer/162106
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-18
  • 2021-09-17
  • 1970-01-01
  • 2013-06-07
  • 2015-08-26
  • 1970-01-01
相关资源
最近更新 更多