User & Role Management API Call Examples in Python (RBAC)
Similar to the User and Group API, Sites that have RBAC enabled can use the User and Role Management APIs for RBAC allows us to create, read, update, and destroy users and roles programmatically. Here's the official documentation.
Below are simple examples showing how you can test this out in Python 3.
To use these APIs, you'll need your site name, api-key, and access to the Users & Roles API for RBAC (upgraded feature)
Users
- GET Users
- GET Single User
- CREATE User
- UPDATE User
- DELETE User
Roles
- GET Roles
- GET Single Role
- CREATE Role
- UPDATE Role
- DELETE Role
Dashboard
- GET Dashboards
USERS
GET Users
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } url = 'https://api.periscopedata.com/api/v2/users' response = requests.get(url, headers=headers) print(json.loads(response.text))
GET Single User
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } url = 'https://api.periscopedata.com/api/v2/users/0000-test-1111-user-id' response = requests.get(url, headers=headers) print(json.loads(response.text))
CREATE User
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } payload = { "first_name": 'MyFirstName' , "last_name": 'MyLastName', "email": 'newuseremail@website.com', "roles": [ '0000-admin-1111-id' ], "invited_by_email": 'useremail@website.com' } data = json.dumps(payload) url = 'https://api.periscopedata.com/api/v2/users?test_mode=true' response = requests.post(url, headers=headers, data=data) print(json.loads(response.text))
UPDATE User
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } payload = { "first_name": 'newFirstName' , "last_name": 'newLastName' } data = json.dumps(payload) url = 'https://api.periscopedata.com/api/v2/users/0000-test-1111-user-id?test_mode=true' response = requests.put(url, headers=headers, data=data) print(json.loads(response.text))
DELETE User
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } url = 'https://api.periscopedata.com/api/v2/users/0000-test-1111-user-id?test_mode=true' response = requests.delete(url, headers=headers) print(response.status_code)
Roles
GET Roles
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } url = 'https://api.periscopedata.com/api/v1/roles' response = requests.get(url, headers=headers) print(json.loads(response.text))
GET Single Role
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } url = 'https://api.periscopedata.com/api/v1/role/0000-test-1111-role-id' response = requests.get(url, headers=headers) print(json.loads(response.text))
CREATE Role
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } payload = { "name": "myNewRole" , "description": "testing", "created_by_email": "useremail@website.com", "privileges": [{ "object_type":"Dashboard", "permissions":["read_dashboards"] }] , "permissions": [{ "object_type":"Dashboard", "object_id":"0000-dashboard-1111-id", "permissions":["create_dashboards"] }] } data = json.dumps(payload) url = 'https://api.periscopedata.com/api/v1/roles?test_mode=true' response = requests.post(url, headers=headers, data=data) print(json.loads(response.text))
UPDATE Role
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } payload = { "name": "myExistingRole" , "description": "NewDescription", "updated_by_email": "useremail@website.com" } data = json.dumps(payload) url = 'https://api.periscopedata.com/api/v1/roles/0000-test-1111-role-id?test_mode=true' response = requests.put(url, headers=headers, data=data) print(json.loads(response.text))
DELETE Role
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json', } url = 'https://api.periscopedata.com/api/v1/roles/0000-test-1111-role-id?test_mode=true' response = requests.delete(url, headers=headers) print(response.status_code)
Dashboards
GET Dashboards
import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } url = 'https://api.periscopedata.com/api/v1/dashboards' response = requests.get(url, headers=headers) print(json.loads(response.text))
Here's an example script on how to create users in bulk from a CSV file that contains the users' names and emails
Note: You'll need to get the role_id's from using the GET Roles call. That will give you the info for all of the site's roles and you'll want to extract the id's from there.
If the role parameter is left out in the payload, then the users will automatically be only in the Everyone role when added.
import requests import json import pandas as pd #insert csv file that contains names and emails df = pd.read_csv('csv file') #use get roles api call to get role id roles = 'role id' invited = 'site admins email' #split first and last name if they are together in one column df['last_name'] = df['Name'].str.split().str[1] df['first_name'] = df['Name'].str.split().str[0] #keep these variables blank first_name = '' last_name = '' email = '' #loop through each user in csv and create user for index, row in df.iterrows(): first_name = row['first_name'] last_name = row['last_name'] email = row['Email'] #insert site name and api_key headers = { 'HTTP-X-PARTNER-AUTH': 'site_name:api_key', 'Content-Type' : 'application/json' } payload = { "first_name": first_name, "last_name": last_name, "email": email, "roles": [ roles ], "invited_by_email": invited } data = json.dumps(payload) #use test_mode=true to test script url = 'https://api.periscopedata.com/api/v2/users?test_mode=false' response = requests.post(url, headers=headers, data=data) print(payload) print(json.loads(response.text))
Please sign in to leave a comment.
Comments
0 comments