0

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'
Trey6
  • 67
  • 2
  • 8
  • have you tried using try..except? – Jason E. May 04 '21 at 23:01
  • thats what im using now, and that prevents it from crashing. so thats a start. Id just think Id be able to get a 200 status code or an error message back from google – Trey6 May 05 '21 at 03:52

0 Answers0