I was managing users using GSuite (GoogleApps) and had to input a large amount of data. I often saw articles such as reading data in the tutorial, but there were not many articles about registration, so I will write an article if it helps.
Create a project with an appropriate name from the Developer Console.
Go back to the dashboard, select the project you created and add the API.
Please enable "Admin SDK" and create an authentication key. --Create credentials with OAuth --Application type: Enter the name appropriately in "Other"
An authentication key will be created and displayed in the list. Download it and save it in a suitable location. (In the code example: Save it as "client_secret_test.jso.json" under [your home directory] /.test_secrets/)
Almost ready to work.
create.rb
# -*- coding: utf-8 -*-
import os
import httplib2
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
def main():
try:
import argparse
parser = argparse.ArgumentParser(parents=[tools.argparser])
flags = parser.parse_args()
except ImportError:
flags = None
#Directory for storing authentication information "."Credentials" setting. Create if directory does not exist
credential_dir = os.path.join(os.path.expanduser('~'), '.test_credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
#Set and read the path of the authentication file
credential_path = os.path.join(credential_dir, 'admin-directory_v1_test.json')
store = Storage(credential_path)
credentials = store.get()
#Create if there is no authentication file
if not credentials or credentials.invalid:
#Set the range of functions to use
scopes = [
'https://www.googleapis.com/auth/admin.directory.user',
]
#Authentication key setting
secret_dir = os.path.join(os.path.expanduser('~'), '.test_secrets')
if not os.path.exists(secret_dir):
os.makedirs(secret_dir)
#Create an instance of the class that performs authentication processing from the authentication key
flow = client.flow_from_clientsecrets(
os.path.join(secret_dir, 'client_secret_test.json'), scopes)
#Application name
flow.user_agent = 'User register Test Tool'
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Python 2.6 Compatibility processing
credentials = tools.run(flow, store)
print('I saved the certificate:' + credential_path)
#Authenticate
http = credentials.authorize(httplib2.Http())
app_admin_service = discovery.build('admin', 'directory_v1', http=http)
#Create user information to create
register_data = {
'primaryEmail':'[email protected]',
'name':{
'givenName':'Taro',
'familyName':'Yamada',
},
'suspended':False, #Do not put account suspended
'password':'p-a-s-s-w-o-r-d',
}
#Registration execution
results = app_admin_service.users().insert(body=register_data).execute()
if(int(results['id']) > 0):
print("Create a google account!")
else:
print("Failed to create google account")
if __name__ == '__main__':
main()
When you run the program, the browser will launch and you will be asked for permission. Click OK to run it.
G Suite Admin SDK Directory API
Recommended Posts