Python 通过谷歌目录API创建活动用户

Python 通过谷歌目录API创建活动用户,python,google-admin-sdk,google-directory-api,Python,Google Admin Sdk,Google Directory Api,我是我的谷歌域名的管理员,我正在尝试自动配置新用户。我首先尝试使用一个名为GAM的命令行工具来实现这一点 当这个工具工作时,我创建的用户中至少有一半被标记为异常活动,并被创建为挂起。我尝试过在各种组合中设置标志,但得到了相同的结果 我还试图通过编写python程序直接使用目录API,但所有新用户仍被创建为挂起用户。这是我的节目。有人能解释一下吗?多谢各位 from __future__ import print_function import httplib2 import os from

我是我的谷歌域名的管理员,我正在尝试自动配置新用户。我首先尝试使用一个名为GAM的命令行工具来实现这一点

当这个工具工作时,我创建的用户中至少有一半被标记为异常活动,并被创建为挂起。我尝试过在各种组合中设置标志,但得到了相同的结果

我还试图通过编写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中关于以下内容的声明:

如果Google帐户已购买邮件许可证,则新用户 帐户将自动分配一个邮箱。这项任务可能需要 几分钟后完成并激活

所以这可能需要一些时间。用户还应注意:

谷歌会自动暂停一个新帐户,直到首次注册 激活帐户的管理员登录。帐户被删除后 激活后,帐户不再被暂停


因此,创建新帐户后,请尝试登录以正式激活它。

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