Google Admin SDK로 사용자 등록 (python)
소개
GSuite(GoogleApps)를 이용하여 사용자 관리를 하고 있어 대량의 데이터 투입을 할 필요가 있었습니다. 데이터의 읽기등의 기사는 튜토리얼로 자주(잘) 보았습니다만, 등록에 관해서 별로 기사가 없었기 때문에 , 뭔가 도움이 되면 라고 기사를 씁니다.
코드를 작성하기 전에 필요한 사항
프로젝트 등록
개발자 콘솔 에서 프로젝트 적절한 이름으로 만듭니다.

대시보드로 돌아가서 만든 프로젝트를 선택하고 API를 추가합니다.

Admin SDK를 활성화하여 인증 키를 만듭니다.
- OAuth로 자격 증명 만들기
- 어플리케이션 종류 : '기타'로 이름은 적당히 넣는다

인증 키가 생성되어 목록에 표시되므로 다운로드하여 적절한 위치에 저장합니다.
(코드 예에서는 : [내 홈 디렉토리]/.test_secrets/아래에 "client_secret_test.jso.json"으로 저장합니다)
대체로 이것으로 움직일 준비가 되었습니다.
실제 코드
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
# 認証情報を格納するディレクトリ「.credentials」の設定。ディレクトリが無い場合は作成
credential_dir = os.path.join(os.path.expanduser('~'), '.test_credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
# 認証ファイルのパスを設定と読み込み
credential_path = os.path.join(credential_dir, 'admin-directory_v1_test.json')
store = Storage(credential_path)
credentials = store.get()
# 認証ファイルが無い場合は作成
if not credentials or credentials.invalid:
# 使用する機能の範囲を設定
scopes = [
'https://www.googleapis.com/auth/admin.directory.user',
]
# 認証キーの設定
secret_dir = os.path.join(os.path.expanduser('~'), '.test_secrets')
if not os.path.exists(secret_dir):
os.makedirs(secret_dir)
# 認証キーから認証処理を行うクラスのインスタンスを生成
flow = client.flow_from_clientsecrets(
os.path.join(secret_dir, 'client_secret_test.json'), scopes)
# アプリケーションの名前
flow.user_agent = 'User register Test Tool'
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Python 2.6 互換用処理
credentials = tools.run(flow, store)
print('証明書を保存しました: ' + credential_path)
# 認証を行う
http = credentials.authorize(httplib2.Http())
app_admin_service = discovery.build('admin', 'directory_v1', http=http)
# 作成するユーザー情報を作成
register_data = {
'primaryEmail':'[email protected]',
'name':{
'givenName':'太郎',
'familyName':'山田',
},
'suspended':False, # アカウント停止の状態にしない
'password':'p-a-s-s-w-o-r-d',
}
# 登録実行
results = app_admin_service.users().insert(body=register_data).execute()
if(int(results['id']) > 0):
print("googleアカウントを作成完了!")
else:
print("googleアカウントを作成に失敗しました")
if __name__ == '__main__':
main()
프로그램을 실행하면 브라우저가 시작되고 사용 권한이 필요하므로 확인을 클릭하여 실행합니다.

참고
G Suite Admin SDK Directory API
Reference
이 문제에 관하여(Google Admin SDK로 사용자 등록 (python)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/naoaki_kaito/items/958ecbe3217ba400ffcf텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)