Google Admin SDK error Resource Not Found: domain when trying to list existing users

自作多情 提交于 2019-12-08 02:52:00

问题


I am trying to write a simple script to get a list of my Google Apps users using Google's python API. So far it looks like this (based on a Google example):

!/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']

The service account has been authorized on google developer console for the following API's:

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

However, when I run the code, I get this error:

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"> 

Any hints on what am I missing?

E.


回答1:


Even when using a service account, you still need to "act as" a Google Apps user in the domain with the proper rights (e.g. a super admin). Try:

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

where admin@domain.com is the email of a super admin in your domain.



来源:https://stackoverflow.com/questions/27175867/google-admin-sdk-error-resource-not-found-domain-when-trying-to-list-existing-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!