I am trying to build a python script that access the email and last logged in time data of all my users on my auth0 portal. How should i approach this? this is the code i have so far
the first part works fine, second part returns the 400 HTTPError.
im trying to have this return a csv file with all the information of users listed, and populate the database automatically with that.
also found some useful links here which i tried incorporating into my code: Bulk User Exports
import sys
sys.path.append(“D:\home\site\wwwroot\env\Lib\site-packages”)
import quickbase
import urllib
import urllib2
import smtplib
import httplib
import pypyodbc
import timestring
import datetime
import time
import math
import sys
import locale
import json
from urllib import urlencode
from urllib2 import urlopen, Request
from dateutil.relativedelta import relativedelta
#Activate Webhook with Campaign is added or modified.
#0.1 establish connection to SQL server
connection = pypyodbc.connect(‘Driver={SQL Server Native Client 11.0};’
‘Server=tcp:xxxxe.database.windows.net,1433;’
‘Database=LQDC;’
‘uid=Admin@lxx;pwd=xxxx’)
cursor = connection.cursor()
#0.2 establish connection to Auth0
def main():
#Configuration Values
AUDIENCE = “https://xxe.auth0.com/api/v2/”
DOMAIN = “lxxx.auth0.com”
CLIENT_ID = “xxxxxL”
CLIENT_SECRET = “xxxxxxr”
GRANT_TYPE = “client_credentials” # OAuth 2.0 flow to use
#Get an Access Token from Auth0
base_url = “https://{domain}”.format(domain=DOMAIN)
data = urllib.urlencode([(‘client_id’, CLIENT_ID),
(‘client_secret’, CLIENT_SECRET),
(‘audience’, AUDIENCE),
(‘grant_type’, GRANT_TYPE)])
req = urllib2.Request(base_url + “/oauth/token”, data)
response = urllib2.urlopen(req)
oauth = json.loads(response.read())
access_token = oauth[‘access_token’]
#Get all Applications using the token
req = urllib2.Request(base_url + “/api/v2/clients”)
req.add_header(‘Authorization’, 'Bearer ’ + access_token)
req.add_header(‘Content-Type’, ‘application/json’)
post_fields = {'connection_id' : 'xxxxxxxx', 'format' : 'csv', 'limit' : '4', "fields" : [{ "name": "email"},{"name": "last_ip"}, { "name": "created_at"}, { "name": "last_login"}] }
req2 = urllib2.Request((base_url + "/api/v2/jobs/users-exports"),urlencode(post_fields).encode())
req2.add_header('Authorization', 'Bearer ' + access_token)
req2.add_header('Content-Type', 'application/json')
try:
response = urllib2.urlopen(req)
res = json.loads(response.read())
print res
response2 = urllib2.urlopen(req2)
result = json.loads(response.read())
print result
except urllib2.HTTPError, e:
print 'HTTPError = ’ + str(e.code) + ’ ’ + str(e.reason)
except urllib2.URLError, e:
print 'URLError = ’ + str(e.reason)
except urllib2.HTTPException, e:
print ‘HTTPException’
except Exception:
print ‘Generic Exception’
#Standard boilerplate to call the main() function.
if name == ‘main’:
main()