如何使用此信息在 Python 中检索返回的数据



我正在尝试使用 auth0,我对如何在 python 中运行此 post 请求有点困惑。 使用这些信息在python/flask中会是什么样子?

POST https://YOUR_DOMAIN/oauth/token
Content-Type: application/x-www-form-urlencoded
audience=API_IDENTIFIER&grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET

它应该返回以下内容:

HTTP/1.1 200 OK
Content-Type: application/json
{
"access_token":"eyJz93a...k4laUWw",
"token_type":"Bearer",
"expires_in":86400
}

如何在 Python 中获取此信息?

这是我正在使用的代码。 它可以在邮递员中工作,但在我的浏览器中我收到此错误:

socket.gaierror
socket.gaierror: [Errno -2] Name or service not known
@app.route('/login')
def get_token():
conn = http.client.HTTPSConnection("")
payload = {'grant_type':'client_credentials',
'client_id':'JXHzBwF6DPiXU2fBjPe1Nd7bYPC6vZ0o',
'client_secret':'aSEqerZw31L19r9QzdcbrLBIVY3i2WD3U6Cd2kBwY0MIKWJrlMNny6A7nySzlSS1',
'audience':'image'
}
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "https://dcadventuresonline.us.auth0.com/oauth/token", headers, payload)
res = conn.getresponse()
data = res.read()

print(data)
return data
import requests

def get_token():
payload = {
"grant_type": "client_credentials",
"client_id": "JXHzBwF6DPiXU2fBjPe1Nd7bYPC6vZ0o",
"client_secret": "aSEqerZw31L19r9QzdcbrLBIVY3i2WD3U6Cd2kBwY0MIKWJrlMNny6A7nySzlSS1",
"audience": "image",
}
headers = {"content-type": "application/x-www-form-urlencoded"}
url = "https://dcadventuresonline.us.auth0.com/oauth/token"
response = requests.post(url=url, headers=headers, data=payload)
return response.json()

最新更新