Creating active users via Google Directory API
<p>I am the admin for my Google domain and I am trying to automate provisioning of new users. I first attempted to do so using a command line tool called GAM.</p>

<p><a href="https://github.com/jay0lee/GAM" rel="nofollow">https://github.com/jay0lee/GAM</a></p>

<p>While this tool works, at least half of the users I create are flagged for unusual activity and are created as suspended. I have tried setting the flags in a variety of combinations but am getting the same result.</p>

<p>I also tried to use the Directory API directly by writing a python program but all new users are still being created as suspended users. Here is my program. Can anyone shed some light on this? Thank you.</p>

<pre><code>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()
</code></pre>

<p>Take note of the following statement from Directory API about <a href="https://developers.google.com/admin-sdk/directory/v1/guides/manage-users" rel="nofollow">Creating accounts</a>:</p>

<blockquote>
  <p>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.</p>
</blockquote>

<p>So it may take sometime. Also note from the user <a href="https://developers.google.com/admin-sdk/directory/v1/reference/users" rel="nofollow">resources</a> that:</p>

<blockquote>
  <p>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.</p>
</blockquote>

<p>So after creating the new account, try logging it in to officially activate it.</p>

python google-admin-sdk google-directory-api