Skip to content

Instantly share code, notes, and snippets.

Created August 13, 2019 18:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dismantl/d52595387ca0fd8934871cf740cb72b0 to your computer and use it in GitHub Desktop.
Export active Okta users to CSV file
# Add your Okta API key and domain here:
# Write CSV headers
echo '"userType","department","firstName","lastName","nickName","displayName","email","title","division","postalAddress","streetAddress","city","state","zipCode","primaryPhone","secondEmail","mobilePhone"' \
# Fetch all active Okta users, convert from JSON to CSV, sort
curl -s -S -X GET -H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: SSWS ${OKTA_API_KEY}" \
--data-urlencode "filter=status eq \"ACTIVE\"" \
"https://${OKTA_DOMAIN}/api/v1/users" \
| jq -r '.[].profile|[.userType, .department, .firstName, .lastName, .nickName, .displayName, .email, .title, .division, .postalAddress, .streetAddress, .city, .state, .zipCode, .primaryPhone, .secondEmail, .mobilePhone]|@csv' \
| sort \
Copy link

dismantl commented Aug 13, 2019

The only dependencies are curl and jq:

Copy link

gabrielsroka commented Aug 15, 2019

Hi @dismantl

This is great! Thanks for sharing. Will it paginate? And observe rate limits?

Keep in mind my PowerShell module can also work on Mac and Linux. Open source ftw.

Copy link

Here's a version that paginates.

Running curl with the -i or --include option includes the response headers.

#!/usr/bin/env bash

# Set these:

# Pagination code based on
while [ "$url" ]; do
    r=$(curl -i --compressed -Ss -H "authorization: SSWS $token" "$url" | tr -d '\r')
    headers=$(echo "$r" | sed '/^$/q')
    body=$(echo "$r" | sed '1,/^$/d')
    echo "$body" | jq -r '.[].profile.login'
    url=$(echo "$headers" | sed -n -E 's/link: <(.*)>; rel="next"/\1/pi')

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