错误-AttributeError:“资源”对象没有属性“用户”

时间:2019-08-28 13:17:58

标签: python google-admin-sdk google-directory-api

我正在使用Gsuite,并试图从我的Gsuite管理员帐户中获取用户。我正在使用Admin-SDK。我正在

上的教程

https://developers.google.com/admin-sdk/directory/v1/quickstart/python

但是当我运行它时,我得到了错误。 AttributeError:“资源”对象没有属性“用户”

我是Gsutie和admin SDK的新手。感谢任何帮助。

我尝试了不同的方法来解决此问题,但没有一个起作用。

from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from googleapiclient import discovery
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/admin.directory.user']

def main():

"""Shows basic usage of the Admin SDK Directory API.
Prints the emails and names of the first 10 users in the domain.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
    with open('token.pickle', 'rb') as token:
        creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
    if creds and creds.expired and creds.refresh_token:
        creds.refresh(Request())
    else:
        flow = InstalledAppFlow.from_client_secrets_file(
            'credentials-new.json', SCOPES)
        creds = flow.run_local_server(port=0)
    # Save the credentials for the next run
    with open('token.pickle', 'wb') as token:
        pickle.dump(creds, token)

service = build('admin', 'directory_v1', credentials=creds)

# 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())
#results = service.AdminDirectory.Users.list()
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()

实际结果应该是Json格式的用户列表。

0 个答案:

没有答案