In order to make the HTTP request to the Auth0 /oauth/token API endpoint we will use the libraries json, urllib and urllib2.This is our sample implementation:
def main(): import json, urllib, urllib2, httplib # Configuration Values domain = "{yourDomain}" # Your Auth0 Domain api_identifier = "API_IDENTIFIER" # API Identifier of your API client_id = "{yourClientId}" # Client ID of your Machine-to-Machine Application client_secret = "{yourClientSecret}" # Client Secret of your Machine to Machine Application api_url = "http://localhost:8080/timesheets/upload" 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': api_identifier, 'grant_type': grant_type}) req = urllib2.Request(base_url + "/oauth/token", data, headers={"Accept": "application/x-www-form-urlencoded"}) response = urllib2.urlopen(req) resp_body = response.read() oauth = json.loads(resp_body) access_token = oauth['access_token']# Standard boilerplate to call the main() function.if __name__ == '__main__': main()
To test this modify your code to print the access_token variable and run the process using python cron.py.