Попытка доступа к Google Directory API через p12 выдает не авторизованную ошибку

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
import json
import base64
import httplib2

f = file('tara1-553d334b8702.p12', 'rb')
key = f.read()
f.close()

credentials = ServiceAccountCredentials.from_p12_keyfile(
    'googleapptara@tara1-167105.iam.gserviceaccount.com',
    'tara1-554d335b8702.p12',
    private_key_password='notasecret',
    scopes=['https://www.googleapis.com/auth/admin.directory.user.readonly','https://www.googleapis.com/auth/admin.directory.user'] 
)

delegated_credentials=credentials.create_delegated('tara.gurung@orgemail.net)

http = httplib2.Http()
http = credentials.authorize(http)


DIRECOTORY = build('admin', 'directory_v1', credentials=credentials)
maxResults=10,orderBy='email').execute()
results = DIRECOTORY.users().list(domain='domainnamehere.net').execute()
users = results.get('users', [])

print users

Здесь я пытаюсь выполнить аутентификацию сервер-сервер, используя файл безопасности p12, и пытаюсь собрать всех пользователей в домене.

Я успешно получил список пользователей с помощью аутентификации 3legs, авторизуясь из браузера в той же учетной записи

Но, таким образом, я получаю следующие ошибки.

File "testing.py", line 41, in <module>
    results = DIRECOTORY.users().list(domain='domainemailhere.net').execute()
  File "/home/tara/taraproject/scripttesting/virtualenvforGapi/local/lib/python2.7/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/tara/taraproject/scripttesting/virtualenvforGapi/local/lib/python2.7/site-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?domain=nepallink.net&alt=json returned "Not Authorized to access this resource/api">

НАСТРОЙКА СДЕЛАНА:

  1. У меня есть супер уровень доступа администратора в консоли администратора.

  2. Я также добавил область через безопасность>showmore>advance>manageipclient>authorize Добавил идентификатор пользователя и область

    https://www.googleapis.com/auth/admin.directory.user.readonly https://www.googleapis.com/auth/admin.directory.user

  3. Добавлено разрешение пользователей в сервисной консоли и сделано владельцем.

  4. Admin SDK включен

Где именно я скучаю по вещам. Почему говорится, что у меня нет прав доступа к ресурсам / API

1 ответ

Решение

Я вижу, что вы используете Delegated_credentials. Вы использовали это??

Изменить следующую строку:

DIRECOTORY = build('admin', 'directory_v1', credentials=credentials)

в

DIRECOTORY = build('admin', 'directory_v1', credentials=delegated_credentials)