I am calling to google admin sdk. I am successfully creating users and everything is working fine. I started testing the project and when I create a duplicate user the program crashes. Can you help me get the status code response from google so I can prevent my code from crashing?
in google im using the user.insert method
Error from creating duplicate user
googleapiclient.errors.HttpError: <HttpError 409 when requesting
https://admin.googleapis.com/admin/directory/v1/users?alt=json returned
"Entity already exists.". Details: "Entity already exists.">
So how do i capture the 409, or a 200 or any other code that would come across?
def create_google_user(first_name, last_name, department):
# all the set up code from google here.
new_email = first_name + '.' + last_name + '@company.com'
new_user = {
"name": {
"familyName": last_name, # Last name
"givenName": first_name, # First name
},
"password": "random_PassWord",
"primaryEmail": new_email,
"suspended": False,
"changePasswordAtNextLogin": True,
"organizations": [
{
"department": department
}
],
"phones": [
{
"value": "1997915801",
"type": "work"
}
],
}
result = service.users().insert(body=new_user).execute()
print('Result: ', result)
print 'User Created'