为什么我的示例 google api 代码上出现 503 服务不可用?

我有一个管理员 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)



慕婉清6462132
浏览 453回答 1
1回答

慕侠2389804

问题是我错过了设置委托,正如DalmTo 所提到的。所以这是我完整的工作代码:from __future__ import print_functionfrom google.oauth2 import service_accountimport googleapiclient.discoverySCOPES = ['https://www.googleapis.com/auth/admin.directory.user', ]SERVICE_ACCOUNT_FILE = './quickstart-1570011757324-2bfbc3d902b9.json'def main():&nbsp; &nbsp; credentials = service_account.Credentials.from_service_account_file(&nbsp; &nbsp; &nbsp; &nbsp; SERVICE_ACCOUNT_FILE, scopes=SCOPES)&nbsp; &nbsp; delegated_credentials = credentials.with_subject('admin@najah.edu')&nbsp; &nbsp; service = googleapiclient.discovery.build('admin', 'directory_v1', credentials=delegated_credentials)&nbsp; &nbsp; # Call the Admin SDK Directory API&nbsp; &nbsp; print('Getting the first 10 users in the domain')&nbsp; &nbsp; results = service.users().list(customer='my_customer', maxResults=10, orderBy='email').execute()&nbsp; &nbsp; users = results.get('users', [])&nbsp; &nbsp; if not users:&nbsp; &nbsp; &nbsp; &nbsp; print('No users in the domain.')&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print('Users:')&nbsp; &nbsp; &nbsp; &nbsp; for user in users:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(u'{0} ({1})'.format(user['primaryEmail'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user['name']['fullName']))if __name__ == '__main__':&nbsp; &nbsp; main()注意credentials.with_subject('admin@domain.com')部分。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python