如何以Python编码形式获取用户身份验证令牌并使用Python接收JWT令牌?

时间:2019-07-22 12:47:23

标签: python-3.x python-requests

我正在尝试通过以URL编码形式发布用户名,密码并接收JWT令牌来生成用户身份验证令牌。

我尝试使用有效负载代替标头和两者,但是没有任何效果。我正在使用Python 3.7。

import requests

headers = {'username':'username', 'password':'password'}
r = requests.post("https://sso.xxxxxx.com/sso-api/v1/token", headers=headers)
print (r)

我希望输出响应200令牌已成功生成,但是我收到错误响应404

1 个答案:

答案 0 :(得分:0)

我能够从xbound那里获得提示和帮助。除了标题以外,我还必须将其作为数据传递。然后我确认响应正常,并提取了令牌。

import requests
payload = {'username':'*******@*********.com', 'password':'**********', 'grant_type':'password', 'scope':'openid'}
r = requests.post("https://***.******.com/***/**/token", data = payload)

##Confirm that reponse is OK!
r.status_code
print(r.status_code == requests.codes.ok)

##Decode json reponse - token
data = r.json()

##Select token
my_token = data['id_token']