2

Google の python API を使用して Google Apps ユーザーのリストを取得する簡単なスクリプトを作成しようとしています。これまでのところ、次のようになっています (Google の例に基づく)。

!/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 = 'service_account_email@developer.gserviceaccount.com'
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 の Google 開発者コンソールで承認されています。

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

ただし、コードを実行すると、次のエラーが発生します。

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.

4

1 に答える 1

3

サービス アカウントを使用する場合でも、適切な権限を持つドメイン内の Google Apps ユーザーとして「行動する」必要があります (スーパー管理者など)。試す:

credentials = SignedJwtAssertionCredentials(client_email, private_key,
                        OAUTH_SCOPE, sub='admin@domain.com')

ここで、admin@domain.com はドメインの特権管理者のメールです。

于 2014-11-28T00:20:24.863 に答える