Retrieving Auth0 User information - python

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
import sys
sys.path.append("D:\home\site\wwwroot\env\Lib\site-packages")
import quickbase
import urllib2
import pypyodbc
import timestring
import datetime
import time
import math
import sys
import locale
import smtplib
import pypyodbc
import httplib
import json
import urllib
import urllib2
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')
try:
response = urllib2.urlopen(req)
res = json.loads(response.read())
print res
conn = httplib.HTTPSConnection(DOMAIN)
payload = "{\"connection_id\":\"not sure which API/token/code to put here\",\"format\": \"csv\", \"limit\": 4, \"fields\": [{ \"name\": \"email\"},{\"name\": \"last_ip\"}, { \"name\": \"created_at\"}, { \"name\": \"last_login\"}]}"
headers = {
'authorization': "Bearer "+access_token,
'content-type': "application/json"
}
conn.request("POST", "/xxxx.auth0.com/api/v2/jobs/users-exports", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
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()
the first part works fine, second part returns the error below
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:
https://auth0.com/docs/users/guides/bulk-user-exports
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /lqdfinance.auth0.com/api/v2/jobs/users-exports</pre>
</body>
</html>

As i can see i your code, you need a connection id so you can use
base_url + '/api/v2/connections'
to get your connection id
Next you need to modify your payload body as below
{
"connection_id": "your_id_as_above",
"format": "csv",
"limit": 5,
"fields": [
{
"name": "user_id"
},
{
"name": "name"
},
{
"name": "email"
},
{
"name": "identities[0].connection",
"export_as": "provider"
},
{
"name": "user_metadata.some_field"
}
]
}
In order to test your response data i suggest you test your api payload and response using Postman, as it gives you more clarity on how the request is working.

Related

How to get python data out of a string

I am setting up a lambda function to put data into a db. I am using API gateway to control the endpoint.
The request I am making sends the data as a string:
fetch(
url,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ name: "hello", contact: "Tim", phone: "555" }),
}
);
My lambda function keeps erroring out because it doesn't know how to handle the string.
My function is:
import re
import psycopg2
import os
import json
def lambda_handler(event, context):
password = os.environ['DB_SECRET']
host = os.environ['HOST']
connection = psycopg2.connect(user="postgres", password=password,
host=host, port="5432",
database="postgres")
cursor = connection.cursor()
postgres_insert_query = "INSERT INTO clients (name, phone, contact) VALUES ('{0}','{1}','{2}')".format(event.get('name'), event.get('phone'), event.get('contact'))
cursor.execute(postgres_insert_query)
print(cursor.rowcount)
print(event['body'])
connection.commit()
return {
'statusCode': 200,
'headers': {
"Access-Control-Allow-Origin" : "*"
},
}
Now my print statement print(event['body']) returns '{"name":"hello","contact":"Tim","phone":"555"}' which is the exact data I need. However for the life of me I can't figure out how to get the data out of the string.
import json
data = json.loads(event['body'])
print(data['name'])

how to pass variables in the email body from lambda aws -python

I am trying to send email from lambda by passing variable in the html body inside payload(in send_email()).I tried using +str(latesturgency)+ and also using {latestimpact} .This doesn.t work.I am new to lambda and python.
How can I solve this?
import json
import logging
import re
import http.client
import mimetypes
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
def send_email(email,latestdescription,latestservice,latestimpact,latesturgency):
conn = http.client.HTTPSConnection("mail.us-east-1.aws.cloud.bmw")
payload = {
"from":"xxx#yyy.com",
"to": "xxx#yyy.com",
"subject": "Test mail","textbody": "body",
"htmlbody": "<h3>Test body!Please create a +str(latesturgency)+ priority ticket to {latestservice} , with below message:{latestdescription}</h3>"
}
print(payload)
headers = {
'Content-Type': 'application/json'
}
data=json.dumps(payload)
print(data)
conn.request("POST", "", data, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
print("done")
def lambda_handler(event, context):
empidemployee= event['currentIntent']["slots"]["empidemployee"]
latestdescription= event['currentIntent']["slots"]["latestdescription"]
latestservice= event['currentIntent']["slots"]["latestservice"]
latestimpact= event['currentIntent']["slots"]["latestimpact"]
latesturgency= event['currentIntent']["slots"]["latesturgency"]
email=event['currentIntent']["slots"]["email"]
send_email(email,latestdescription,latestservice,latestimpact,latesturgency)
You can do this using the format function fro your string, I have added this below.
import json
import logging
import re
import http.client
import mimetypes
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
def send_email(email,latestdescription,latestservice,latestimpact,latesturgency):
conn = http.client.HTTPSConnection("mail.us-east-1.aws.cloud.bmw")
payload = {
"from":"xxx#yyy.com",
"to": "xxx#yyy.com",
"subject": "Test mail","textbody": "body",
"htmlbody": "<h3>Test body!Please create a {} priority ticket to {}, with below message:{}</h3>".format(latesturgency, latestservice, latestdescription)
}
print(payload)
headers = {
'Content-Type': 'application/json'
}
data=json.dumps(payload)
print(data)
conn.request("POST", "", data, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
print("done")
def lambda_handler(event, context):
empidemployee= event['currentIntent']["slots"]["empidemployee"]
latestdescription= event['currentIntent']["slots"]["latestdescription"]
latestservice= event['currentIntent']["slots"]["latestservice"]
latestimpact= event['currentIntent']["slots"]["latestimpact"]
latesturgency= event['currentIntent']["slots"]["latesturgency"]
email=event['currentIntent']["slots"]["email"]
send_email(email,latestdescription,latestservice,latestimpact,latesturgency)
Use the {} notation to indicate where a variable should be replace then use .format() after your string passing in the variables in the ordering you will use them in.
In your case it is now
"<h3>Test body!Please create a {} priority ticket to {}, with below message:{}</h3>".format(latesturgency, latestservice, latestdescription)
For more information about the format function take a read in the documentation.

The problem with cognitive Service to Identify. I can't write the query correctly

What should I write in the body? And how do I get the name of my person in the personal group in return?
import http.client, urllib, base64, json, requests
headers = {
# Request headers
'Content-Type': 'application/json', # this should be the content type
'Ocp-Apim-Subscription-Key': 'XXXXXXXXXXXXXXXXXXXXXXXXXx',
}
params = {
# Request parameters
"personGroupId": "nikitasyomkin",
"faceIds": [
"6564e492-a09c-4bab-906b-50507bcc9351",
"2dea2b5b-5992-4350-b911-365e6bc06355",
"5f360bd7-b3ed-4840-866f-3f5777e95639"
],
"maxNumOfCandidatesReturned": 1,
"confidenceThreshold": 0.5
}
conn = http.client.HTTPSConnection('XXXXXX.cognitiveservices.azure.com')
conn.request("POST", "/face/v1.0/identify?%s" % params, "body", headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
Based on official API reference , you can not get person's name from Identify API.
If you want to get a person's name via persionID you get , you can call this API :
GET https://XXXXXX.cognitiveservices.azure.com/face/v1.0/persongroups/<personGroupId>/persons/<persionId>

Properly formatting http.client.HTTPSConnection in Python

Overall, I'm trying to invoke the MS Cognitive key phrases API from Python 3.5.1 :: Anaconda 4.0.0 (32-bit). I looked everywhere and tried to incorporate this stackoverflow response.
To call the API your account key below marked as ## needs to be added from here,
however to format the body correctly you probably don't need the account key. A good portion of the code below is from sample code.
Request body should look like
body = {
"documents": [
{
"language": "en",
"id": "1",
"text": "One line of text."
},
{
"language": "en",
"id": "2",
"text": "another line of text."
}
]
}
my code <it now works!!>
import sys
import os.path
import http.client
import urllib.request
import urllib.parse
import urllib.error
import base64
import json
subscription_key = '##'
headers = {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': subscription_key
}
#input text is: ID | text to analyze. How my input file is formatted.
input_text = ["100|One line of text.", "101|another line of text."]
# Inputs holds the params to call the web service in bulk.
body = []
indx = 1
for line in input_text:
input_text = line.split("|")
print ('-----\n')
print ("Input text is:", input_text)
input_text_analyze = input_text[1]
print ('\nInput text to be analyzed:', input_text_analyze)
body.append({ "language" : "en", "id" : str(indx), "text" : input_text_analyze })
indx = indx + 1
print ('-----\n')
print ('\nBody has', body)
print ("Calling API to get keywords...")
body_documents = { 'documents': body }
print ("\nParams:", body_documents)
params = urllib.parse.urlencode({ })
try:
conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')
conn.request("POST", "/text/analytics/v2.0/keyPhrases?%s" % params, str(body_documents), headers)
response = conn.getresponse()
keyword_obj = response.read()
print("Returned keyword_obj is: ", keyword_obj)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
I made 2 changed to the code above that allow it to work. 1) I was getting my params and body mixed up. 2) I needed to add str(body_documents) in my post. Both beginner mistakes.

YouTube API video upload error: parseError /w python

The example code for Google's YouTube Data API is a piece of junk. It's so complicated and tied to the oauth redirect flow that I can't use it. Trying to go raw with requests pip and not getting too far.
I've followed the instructions exactly (as far as I can tell), with the following code:
import json
import os
import sys
import urllib
import requests
payload_file = None
payload = None
print 'Loading Config'
# Get the directory path of this file. When using any relative file paths make
# sure they are relative to current_dir so that the script can be run from any CWD.
current_dir = os.path.dirname(os.path.abspath(__file__))
# Reads in the config.json file then parses it
config = json.loads(open(os.path.join(current_dir, '..', 'config.json')).read())
print 'Parsing Payload'
for i in range(len(sys.argv)):
if sys.argv[i] == "--json" and (i + 1) < len(sys.argv):
payload = json.loads(sys.argv[i + 1])
elif sys.argv[i] == "-payload" and (i + 1) < len(sys.argv):
payload_file = sys.argv[i + 1]
with open(payload_file,'r') as f:
payload = json.loads(f.read())
break
print 'Configuring youtube with token {0}'.format(payload['token'])
print 'Downloading video...'
# See how big it is
f = urllib.urlopen(payload['url'])
content_length = int(f.headers["Content-Length"])
# Download it
# urllib.urlretrieve(payload['url'], "video.mp4")
metadata = {
'snippet' : {
'title': payload['title'],
"categoryId": 22
},
'status' : {
"privacyStatus": "public",
"embeddable": True,
"license": "youtube"
}
}
if 'tags' in payload:
metadata['snippet']['tags'] = payload['tags']
if 'description' in payload:
metadata['snippet']['description'] = payload['description']
headers = {
'Authorization' : 'Bearer {0}'.format(payload['token']),
'Content-Type' : 'application/json; charset=UTF-8',
'Content-Length' : json.dumps(metadata).__len__(),
'X-Upload-Content-Length' : content_length,
'X-Upload-Content-Type' : 'video/*',
}
print 'Attempting to upload video'
print headers
# upload video file
r = requests.post('https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status', data=metadata, headers=headers);
print "RESPONSE!"
print r.text
# files = {
# 'file': video_file,
# }
# r = requests.post('https://www.googleapis.com/upload/youtube/v3/videos', data={ "video" : video }, headers=headers);
Obviously its not finished, but its dying on the metadata upload request with the following output:
Loading Config
Parsing Payload
Configuring youtube with token <access-token>
Downloading video...
Attempting to upload video
{'X-Upload-Content-Length': 51998563, 'Content-Length': 578, 'Content-Type': 'application/json; charset=UTF-8', 'X-Upload-Content-Type': 'video/*', 'Authorization': 'Bearer <access-token>'}
RESPONSE!
{
"error": {
"errors": [
{
"domain": "global",
"reason": "parseError",
"message": "Parse Error"
}
],
"code": 400,
"message": "Parse Error"
}
}
This error is not even listed in their "Errors" docs.
What is wrong with my code?
Here is an example in python that works. It assumes you've already done the oauth part though.
import requests
from os import fstat
import json
fi = open('myvideo.mp4')
base_headers = {
'Authorization': '%s %s' % (auth_data['token_type'],
auth_data['access_token']),
'content-type': 'application/json'
}
initial_headers = base_headers.copy()
initial_headers.update({
'x-upload-content-length': fstat(fi.fileno()).st_size,
'x-upload-content-type': 'video/mp4'
})
initial_resp = requests.post(
'https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status,contentDetails',
headers=initial_headers,
data=json.dumps({
'snippet': {
'title': 'my title',
},
'status': {
'privacyStatus': 'unlisted',
'embeddable': True
}
})
)
upload_url = initial_resp.headers['location']
resp = requests.put(
upload_url,
headers=base_headers,
data=fi
)
fi.close()
the above is graet, just adding: you can also get the youtube id from the response (for future use):
cont = json.loads(resp.content)
youtube_id = cont['id']

Categories