Не удается получить пользователей из G Suite в Python 3

Пытаюсь решить задачу с помощью Admin SDK API - Получить список пользователей из домена - Изменить пароль пользователей

В моем случае я создал пользователя службы в G Suite и написал сценарий из приведенного ниже примера:

from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
from apiclient.discovery import build

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

    credentials = ServiceAccountCredentials.from_json_keyfile_name('paswd.json', scopes=scopes)
    http_auth = credentials.authorize(Http())
    service = build('admin', 'directory_v1', http=http_auth)

    print('Getting the first 10 users in the domain')
    results = service.users().list(customer='my_customer', maxResults=10, orderBy='email', domain='nnn.nn').execute()
    print(results)

if __name__ == '__main__':
    main()

Когда я выполняю скрипт, возникает следующее исключение:

Getting the first 10 users in the domain
Traceback (most recent call last):
  File "./run-2.py", line 25, in <module>
    main()
  File "./run-2.py", line 21, in main
    results = service.users().list(customer='my_customer', maxResults=10, orderBy='email', domain='nnn.nn').execute()
  File "/usr/local/lib/python3.4/dist-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/googleapiclient/http.py", line 840, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/admin/directory/v1/users?customer=my_customer&domain=g.nsu.ru&alt=json&maxResults=10&orderBy=email returned "Not Authorized to access this resource/api">

В чем может быть проблема?


person Виктор Ходорченко    schedule 22.03.2017    source источник
comment
Ваша ошибка похожа на эту тему SO и вроде бы решено.   -  person noogui    schedule 22.03.2017


Ответы (1)


Насколько я смог выяснить, вам не хватает:

credentials = ServiceAccountCredentials.from_json_keyfile_name('paswd.json', scopes=scopes)
delegated_credentials = credentials.create_delegated(DELEGATED_ACCOUNT)
http_auth = delegated_credentials.authorize(Http())

Где DELEGATED_ACCOUNT - администратор вашего домена.

person Ronald Reed    schedule 24.03.2017