1 回答

TA貢獻1719條經驗 獲得超6個贊
問題是我錯過了設置委托,正如DalmTo 所提到的。所以這是我完整的工作代碼:
from __future__ import print_function
from google.oauth2 import service_account
import googleapiclient.discovery
SCOPES = ['https://www.googleapis.com/auth/admin.directory.user', ]
SERVICE_ACCOUNT_FILE = './quickstart-1570011757324-2bfbc3d902b9.json'
def main():
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
delegated_credentials = credentials.with_subject('[email protected]')
service = googleapiclient.discovery.build('admin', 'directory_v1', credentials=delegated_credentials)
# Call the Admin SDK Directory API
print('Getting the first 10 users in the domain')
results = service.users().list(customer='my_customer', maxResults=10, orderBy='email').execute()
users = results.get('users', [])
if not users:
print('No users in the domain.')
else:
print('Users:')
for user in users:
print(u'{0} ({1})'.format(user['primaryEmail'],
user['name']['fullName']))
if __name__ == '__main__':
main()
注意credentials.with_subject('[email protected]')部分。
添加回答
舉報