Getting Connection errors on quickstart chapter to generate Token with Python (M2M)

I am new using Auth0 and I am facing difficulties to generate a token using the snippet available on

I can reproduce the token generation with curl, on command line, but I can’t take the same result with the Python snippet, which is copied bellow, with my attempts:

import httplib, urllib
from decouple import config
AUTH0_CLIENT_SECRET = config("AUTH0_CLIENT_SECRET")
# AUTH0_CLIENT_ID = config("AUTH0_CLIENT_ID")
# AUTH0_AUDIENCE = config("AUTH0_AUDIENCE")
AUTH0_AUDIENCE = config("AUTH0_CLIENT_ID")
AUTH0_API_IDENTIFIER = config("AUTH0_API_IDENTIFIER")

conn = httplib.HTTPSConnection("")
# payload = "grant_type=client_credentials&client_id=" + AUTH0_CLIENT_ID + "&client_secret=" + AUTH0_CLIENT_SECRET + "&audience=" + AUTH0_AUDIENCE
# payload = {"grant_type": "client_credentials", "client_id": AUTH0_CLIENT_ID, 'client_secret': AUTH0_CLIENT_SECRET, 'audience': AUTH0_AUDIENCE}
payload = urllib.urlencode({"grant_type": "client_credentials", "client_id": AUTH0_CLIENT_ID, 'client_secret': AUTH0_CLIENT_SECRET, 'audience': AUTH0_AUDIENCE})
headers = { 'Content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/my-app.us.auth0.com/oauth/token", payload, headers)
res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))
return data

Am i missing something? My answers are one of the following (with different payload values bellow):

  • socket.gaierror: [Errno -5] No address associated with hostname
  • 401 Response
  • gaierror: [Errno -2] Name or service not known

The error is always on line “conn.request(“POST”, “/my-app.us.auth0.com/oauth/token”, payload, headers)”.

The value for the “audience” key seems to be the https://my-app.us.auth0.com/api/v2/ despite the link above tells that it is the API_IDENTIFIER

My snippet is on Python 2, but I had already tried it on Python 3 as well with similar results. Thanks in advance.

Hey there!

Haven’t dived into your code snippets yet but got one question are you strictly following the quickstart and got such errors?

Yes, I tried on Python 3 with exactly the same code, running as a script, as a view on my app and line by line on the python cli.

Gotchya! Let me try reproduce it myself and see where it goes. I’ll get back to you as soon as I have a chance to do it.

The following code worked for me. The strategy was to use the “requests” library instead of httplib/http.client

def generateAuth0Token():
    import ast
    import requests
    from decouple import config
    AUTH0_CLIENT_SECRET = config("AUTH0_CLIENT_SECRET")
    AUTH0_CLIENT_ID = config("AUTH0_CLIENT_ID")
    AUTH0_AUDIENCE = config("AUTH0_AUDIENCE")
    AUTH0_API_IDENTIFIER = config("AUTH0_API_IDENTIFIER")

    headers = { 'content-type': "application/x-www-form-urlencoded" }
    data = {"grant_type": "client_credentials", "client_id": AUTH0_CLIENT_ID, 'client_secret':  AUTH0_CLIENT_SECRET, 'audience': AUTH0_AUDIENCE}
    response = requests.post('https://my-app.us.auth0.com/oauth/token', headers=headers, data=data)
    content = response.content
    content = ast.literal_eval(content)
    token = content.get('access_token')
    return token
1 Like

Perfect! Thanks for sharing that!

The team has been informed about that and they should implement the fix soon.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.