通过 Google 目录 API 创建活跃用户

Creating active users via Google Directory API

我是 Google 域的管理员,我正在尝试自动配置新用户。我首先尝试使用名为 GAM 的命令行工具这样做。

https://github.com/jay0lee/GAM

虽然此工具有效,但我创建的用户中至少有一半被标记为异常 activity 并被创建为已暂停。我已尝试以各种组合设置标志,但得到的结果相同。

我还尝试通过编写 python 程序直接使用目录 API,但所有新用户仍被创建为暂停用户。这是我的程序。任何人都可以阐明这一点吗?谢谢。

from __future__ import print_function
import httplib2
import os

from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = 'https://www.googleapis.com/auth/admin.directory.user'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Directory API Python Quickstart'


def get_credentials():
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                               'admin-directory_v1-python-quickstart.json')

    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials

def create_user():
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('admin', 'directory_v1', http=http)

    userinfo = {'primaryEmail': 'jane@mydomain.com',
        'name': { 'givenName': 'Jane', 'familyName': 'Smith' },
        'password': '34gjklre304iojlo24j2kl3kdlj',}

    service.users().insert(body=userinfo).execute()

if __name__ == '__main__':
    create_user()
    main()

请注意目录 API 中关于 Creating accounts 的以下声明:

If the Google account has purchased mail licenses, the new user account is automatically assigned a mailbox. This assignment may take a few minutes to be completed and activated.

所以这可能需要一些时间。另请注意,用户 resources 指出:

A new account is automatically suspended by Google until the initial administrator login which activates the account. After the account is activated, the account is no longer suspended.

所以创建新账号后,尝试登录正式激活。

此问题最终归因于在免费试用域上进行的测试。使用试用域,GAM 和上面的 python 程序都创建了一些暂停用户和一些活跃用户。但是,当我在我的付费完整版 google 域上尝试这两种方法时,我能够始终如一地创建所有活跃用户。