[英]Using the auth0 /oauth/token endpoint from python
我想弄清楚 Auth0 是如何工作的。 到目前为止,我并不幸运。 我想要实现的目标很简单:POST 到 oauth/token 端点并接收一个令牌作为回报。 使用 curl 没问题。 我可以 curl --request POST [...] 并收到令牌就好了。 当我尝试使用 python 和 pythons 请求模块执行此操作时,我收到一条错误消息,指出我未获得授权。 数据完全一样,我仔细检查了一下。 这是我的 python 代码:
import json, requests
endpoint = 'https://mydomain.auth0.com/oauth/token'
data = {
'grant_type': 'password',
'username': 'myuser',
'password': 'mypassword',
'audience': '',
'scope': 'read:sample',
'client_id': 'myclientid',
'client_secret': 'myclientsecret'
},
headers = {'content-type': 'application/json'}
response = requests.post(url=endpoint, data=json.dumps(data), headers=headers)
print("STATUS: ", response.status_code)
print("Response: ", response.json())
工作 Curl 请求:
curl --request POST \\
--url 'https://mydomain.auth0.com/oauth/token' \\
--header 'content-type: application/json' \\
--data '{"grant_type":"password","username": "myuser","password": "mypassword","audience": "", "scope": "read:sample", "client_id": "myclientid", "client_secret": "myclientsecret"}'
我还尝试将我的 python 代码中的用户代理设置为“curl/7.51.0”,但这也没有用:D
我对任何指针都很满意!
编辑:
我可耻地不得不承认我毕竟没有使用相同的用户名。 这并没有改变我无法使用 python 代码授权自己的事实。 @cricket_007 建议的 SDK 工作正常。 我觉得这有点奇怪,但很好。
我认为这实际上是您使用requests.post()
一个简单错误
url不是关键字参数:
response = requests.post(endpoint, data=json.dumps(data), headers=headers)
应该在这里工作...
我现在的问题是很久以前的了,但这只是data=
的问题,应该用json=
代替
请求变为:
response = requests.post(endpoint, json=data, headers=headers)
你保存一个json.dumps
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.