使用管理员帐户时,G-Suite的哪个API(日历,Gmail,驱动器)可以获取所有员工的Gmail地址?

时间:2019-02-01 02:12:46

标签: google-api google-calendar-api gmail-api

我使用google-calendar-api从link获取日历的事件数据。

参数calendarId是Gmail帐户。

在我的公司中,我有一个GSuite管理员帐户。

我想获得员工的Gmail帐户。

现在,因为我知道某些人的Gmail帐户,例如,我有100个人的帐户。但是,如果有新员工来公司,我不知道这些新员工的Gmail帐户。

因此,如果这些新员工有新的公司gmail,我也可以使用管理员帐户到达那里吗?

哪个api或我可以从哪里获得正式员工的gmail?

1 个答案:

答案 0 :(得分:0)

我可以用这个GSuite Admin SDK Directory API来做。

演示code

from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
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.json', SCOPES)
            creds = flow.run_local_server()
        # 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()
    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()

然后我将Calendar_credentials.json用作credentials.json