【发布时间】:2018-07-06 16:49:27
【问题描述】:
我试图弄清楚 Auth0 的工作原理。到目前为止,我并不幸运。我想要实现的目标很简单:POST 到 oauth/token 端点并接收一个令牌作为回报。使用 curl 这不是问题。我可以 curl --request POST [...] 并收到一个令牌就好了。当我尝试使用 python 和 pythons requests 模块执行此操作时,我收到一条错误消息,指出我未获得授权。数据完全一样,我仔细检查了一遍。这是我的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 --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 工作正常。我觉得这有点奇怪,但很好。
【问题讨论】:
-
你能显示完整的 curl 请求吗?
-
我添加了 curl 请求。
-
标题应该是
Content-Type,但我不知道这是否真的很重要 -
不清楚你为什么要使用请求。 Auth0 有一个 Python SDK github.com/auth0/auth0-python/blob/master/auth0/v3/…
-
是的,我用 SDK 试过了,效果很好!感谢您抽出宝贵时间对此进行调查!
标签: python python-requests auth0