0

I'm getting an error that google doesn't recognize the user names in the json. I cannot find a way to include the http header application/json. The return URL gives me another error code but I pass login authentication.

My code:

from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import requests
import json
import pprint

# Setup the Admin SDK Directory API
SCOPES = 'https://www.googleapis.com/auth/admin.directory.user'
#Global scope for access to all user and user alias operations.
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = build('admin', 'directory_v1', http=creds.authorize(Http()))
# Call the Admin SDK Directory API
userInfo = {
    "name" : {
        "givenName" : "Donald",
        "familyName" : "Dump",
    },
    "kind" : "user",
    "primaryEmail" : "testUser@test.com",
    "isAdmin": False,
    "isDelegatedAdmin": False,
    "agreedToTerms": True,
    "password": "testpass1234",
    "changePasswordAtNextLogin": True  
}
response = service.users().insert(body=json.dumps(userInfo)).execute()

Error code Positional Wrapper _helpers.py Link that produced HttpError 400

dpo
  • 1
  • 3

2 Answers2

0

I'm not experienced with Google APIs, but I believe it's expecting a valid JSON, so we have a problem in the code even before sending the request itself. So, before going any further, you should fix your declared dictionary: Writing "isAdmin": "false", is not okay, because True/False are booleans, however you're sending them as a string. You should write "isAdmin": False, instead. If editing the json file itself, write false, because in python the booleans start with a capital letter, but not in JSON.

Riverman
  • 503
  • 6
  • 17
0

Let me add my 50cc. This code works for me.

admin_user_payload = {
 "status": "true"
 } 

request = service.users().makeAdmin(userKey='admin_user_key', body=admin_user_payload)
admin = request.execute()

where admin_user_key should be for example info@myaccount.com. I mean it's the userKey.

isAdmin in the payload user insert is ignored by default. For making an user admin you should use the method makeAdmin passing userKey (the user email is valid) and the payload with status:true.

Below you can find all API docs.

https://google-api-client-libraries.appspot.com/documentation/admin/directory_v1/python/latest/admin_directory_v1.users.html