Created
September 27, 2016 03:15
-
-
Save raffi-minassian/e04a5c55541509ebed97463b39c6400d to your computer and use it in GitHub Desktop.
Quick and dirty auth0 user export to csv using node.js.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Create a dir, install 3 dependent packages, copy this index.js file to dir, run with node ./index.js > users.csv | |
*/ | |
var async = require('async') | |
var req = require('request') | |
var stringify = require('csv-stringify') | |
var fs = require('fs') | |
var url = "https://your-subdomain.auth0.com/api/v2/users" //your management api endpoint | |
var token = "HGigi7tigugIYUHFUYgi8769uYFI..." //your bearer token | |
var fields = "email,logins_count" //the fields you want returned. see api docs | |
function objectMorpher(user, next){ | |
//process each user record here to flatten it for csv if needed | |
next() | |
} | |
//thats it. code that does the work is below | |
getData(0) | |
function getData(page){ | |
req({ | |
method: "GET", | |
url: url, | |
headers: { | |
"Authorization": "Bearer "+token | |
}, | |
qs: { | |
page: page, | |
per_page: 50, //50 results per request | |
fields: fields, | |
include_fields: true | |
}, | |
json: true | |
}, function(err, res, body){ | |
if(err){ | |
return console.log(err) | |
} | |
async.eachSeries(body, objectMorpher, function(err){ | |
if(err){ | |
return console.log(err) | |
} | |
stringify(body, {header: true}, function(err, output){ | |
if(err){ | |
return next(err) | |
} | |
process.stdout.write(output); | |
if(body.length === 50){ | |
setTimeout(function(){ | |
getData(page+1) | |
}, 5000) //be nice to the auth0 server. pause 5 sec between requests | |
} | |
}) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment