2014-11-27 4 views
2

간단한 스크립트를 작성하여 Google의 Python API를 사용하여 Google Apps 사용자 목록을 가져 오려고합니다. 지금까지 그것은 (구글 예에 따라) 다음과 같습니다 그러나Google Admin SDK 오류 리소스를 찾을 수 없음 : 기존 사용자를 나열하려고 시도 할 때 도메인

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

:

!/usr/bin/python 

import httplib2 

from apiclient import errors 
from apiclient.discovery import build 
from oauth2client.client import OAuth2WebServerFlow 
from oauth2client.client import SignedJwtAssertionCredentials 

client_email = '[email protected]' 
with open("Python GAPS-98dfb88b4c9f.p12") as f: 
    private_key = f.read() 

OAUTH_SCOPE = 'https://www.googleapis.com/auth/admin.directory.user' 

credentials = SignedJwtAssertionCredentials(client_email, private_key, OAUTH_SCOPE) 
http = httplib2.Http() 
http = credentials.authorize(http) 

directory_service = build('admin', 'directory_v1', http=http) 

all_users = [] 
page_token = None 
params = {'customer': 'my_customer'} 

while True: 
    try: 
    if page_token: 
     param['pageToken'] = page_token 
    current_page = directory_service.users().list(**params).execute() 

    all_users.extend(current_page['users']) 
    page_token = current_page.get('nextPageToken') 
    if not page_token: 
     break 
    except errors.HttpError as error: 
    print 'An error occurred: %s' % error 
    break 

for user in all_users: 
    print user['primaryEmail'] 

서비스 계정은 다음과 같은 API의에 대한 구글의 개발자 콘솔에 권한이 부여 된 코드를 실행하면이 오류가 발생합니다.

An error occurred: <HttpError 404 when requesting https://www.googleapis.com/admin/directory/v1/users?customer=my_customer&alt=json returned "Resource Not Found: domain"> 

누락 된 부분에 대한 힌트가 있습니까? E.

답변

3

서비스 계정을 사용하더라도

, 당신은 여전히 ​​적절한 권한을 가진 도메인 (예 : 최고 관리자)에서 Google 애플리케이션 사용자 "와 같은 행위"로해야합니다. 시도 :

credentials = SignedJwtAssertionCredentials(client_email, private_key, 
         OAUTH_SCOPE, sub='[email protected]') 

여기서 [email protected]은 도메인의 최고 관리자 이메일입니다.

+0

우수하고 매력적이었습니다. 감사! – Level15