In this article
- Bulk importing of users through the API using a python script
Required permissions
- API_ACCESS (permits enabling the API)
Instructions
Enabling the API:
The API will need to be enabled for this to work. For instructions on enabling the API see Get an API Key
Performing the import:
1. Please create a CSV file with the following columns. The file be named users.csv.
firstName,lastName,email,username
2. Run the following script to convert it to json:
import csv
import json
with open('users.csv') as infile:
reader = csv.DictReader(infile)
out = [{"firstName": row['firstName'],"lastName": row["lastName"],"email": row["email"],"username": row["username"], "auth": "saml", "roleGroups": [""]} for row in reader]
with open('users.json', 'w') as outfile:
json.dump(out,outfile,sort_keys=True, indent=4)
you can fill in the blank after "roleGroups" to set the default role for all users i.e. "roleGroups": ["ROLE_ADMIN"] then run this file:
import json
import requests
import csv
from api import *
api_endpointUsers = "/api/v1/users"
api_endpoint2 = api_endpoint+api_endpointUsers
headers = {'api-token': api_token}
with open('users.json','r')as infile:
indata = json.load(infile)
output =[]
for data in indata:
response = requests.post(api_endpoint2, headers=headers, json=data)
reason = (response.reason)
reason2 = (response.text)
with open('Response.json','w') as outfile:
json.dump(reason2,outfile,sort_keys=True,indent=4)
print(reason)
print(reason2)
you'll get a reply something like this:
hoffy@LAPTOP-D484PRJU:~/scripts$ python3 createuserFromCSV.py
Created
[]
Created
[]
Created
[]
Created
[]
Created
[]
Created
[]
Created
[]
Created
etc...
Comments
0 comments
Article is closed for comments.