0

I'm trying to create google accounts automatically using the Google Directory API in a Python script. But when I run my script, I get the following error :

Traceback (most recent call last):
  File "./create_user.py", line 34, in <module>
    main()
  File "./create_user.py", line 31, in main
    directory.create_user(userInfo)
  File "/home/romain/dev/ezdrive/access-auto/google_wrapper/directory.py", line 27, in create_user
    self.service.users().insert(body=json.dumps(userInfo)).execute()
  File "/home/romain/dev/ezdrive/access-auto/env/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/romain/dev/ezdrive/access-auto/env/lib/python3.8/site-packages/googleapiclient/http.py", line 943, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://admin.googleapis.com/admin/directory/v1/users?alt=json returned "Invalid Input: primary_user_email". Details: "[{'message': 'Invalid Input: primary_user_email', 'domain': 'global', 'reason': 'invalid'}]">

Here is the code for creating an account :

def main():
    userInfo = {
        "name": {
            "givenName": "Romain",
            "familyName": "Test DEV 1"
        },
        "kind": "user",
        "primaryEmail": "romain.test1@mydomain.net",
        "isDelegatedAdmin": False,
        "suspended": False,
        "isAdmin": False,
        "agreedToTerms": True,
        "password": "Password456",
        "changePasswordAtNextLogin": True
    }
    directory.create_user(userInfo)


class Directory:
    def __init__(self):
        self.get_service()

    def get_service(self):
        creds = oauth_login()
        self.service = build('admin', 'directory_v1', credentials=creds)

    def create_user(self, userInfo):
        self.service.users().insert(body=json.dumps(userInfo)).execute()

I have checked that the primaryEmail was using the right domain name so I don't really understand what's wrong.

Thanks in advance for your help !

Romain L.
  • 21
  • 1
  • 3

1 Answers1

1

Finally I found what my problem was. I read somewhere that I should do service.users().insert(body=json.dumps(userInfo)) but in fact I just had to do service.users().insert(body=userInfo). I guess the JSON conversion already happens automatically behind the curtain.

Romain L.
  • 21
  • 1
  • 3