Skip to content

Instantly share code, notes, and snippets.

@lstroud
Created March 1, 2018 05:18
Show Gist options
  • Save lstroud/e8207d12f26e42b85749326618159a13 to your computer and use it in GitHub Desktop.
Save lstroud/e8207d12f26e42b85749326618159a13 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
#
# List Active Okta Users
#
import requests
import json
import pprint
import csv
token = 'WHATEVER_YOUR_TOKEN_IS'
limit = '200'
base_url = 'https://mycompany.okta.com/api/v1/'
url = base_url + '/users?limit=' + limit
headers = {'Authorization' : 'SSWS ' + token,
'Accept' : 'application/json',
'Content-Type' : 'application/json' }
r = requests.get(url, headers=headers)
json_obj = r.json()
with open('active-users.csv', 'w', newline='') as csvfile:
fieldnames = ['firstName', 'lastName', 'login', 'email']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for json_row in json_obj:
writer.writerow({
'firstName': json_row['profile']['firstName'],
'lastName': json_row['profile']['lastName'],
'login': json_row['profile']['login'],
'email': json_row['profile']['email']
})
@YuvrajKukar
Copy link

Above script works for me but it is limiting to 120 user limit. Can you suggest how to open the pagination limit to retrieve all accounts with this query? Okta article provides no direction on how to use the operators in the query. Can you suggest how to use in this python script to retrieve more than the limit?

@gabrielsroka
Copy link

gabrielsroka commented Nov 28, 2022

@lstroud, @YuvrajKukar, this version paginates

#!/usr/bin/python

# List Active Okta Users

import requests
import csv

token = 'WHATEVER_YOUR_TOKEN_IS'
limit = '200'
url = 'https://mycompany.okta.com/api/v1/users?limit=' + limit

headers = {'Authorization': 'SSWS ' + token,
          'Accept': 'application/json'}

# session is faster!
session = requests.Session()
session.headers.update(headers)

with open('active-users.csv', 'w', newline='') as csvfile:
    fieldnames = ['firstName', 'lastName', 'login', 'email']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    while url:
        r = session.get(url)
        users = r.json()
        for user in users:
            writer.writerow({
                'firstName': user['profile']['firstName'],
                'lastName': user['profile']['lastName'],
                'login': user['profile']['login'],
                'email': user['profile']['email']
            })
        url = r.links.get('next', {}).get('url')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment