尝试使用PyJWT
库生成 JWT 令牌。
当我使用以下程序生成 JWT 令牌时 - 该令牌不起作用。
但是,当我使用 具有相同详细信息的网站https://jwt.io/时 - 令牌有效。有什么我想念的吗。
我需要 python 正确生成令牌,以便我可以自动化一些需要此令牌的 API。
蟒蛇程序:
import jwt
import base64
code = jwt.encode({'sub':'String','nbf':'1501594247',
'exp':'1501767047', 'iss': 'string', 'aud': 'String'},
base64.b64encode('secret'), algorithm='HS256')
print code
Run Code Online (Sandbox Code Playgroud)
例子:
code = jwt.encode({'sub':'AccountNUmber.QTVR','nbf':'1501594247','exp':'1501860089', 'iss': 'client_id', 'aud': 'https://login.google.com/oauth'},
base64.b64encode('secret'), algorithm='HS256')
Run Code Online (Sandbox Code Playgroud)
结果:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjbGllbnRfaWQiLCJhdWQiOiJodHRwczovL2xvZ2luLmdvb2dsZS5jb20vb2F1dGgiLCJzdWIiOiJBY2NvdW50TlVtYmVyLlFUVlIiLCJleHAiOiIxNTAxODYwMDg5IiwibmJmIjoiMTUwMTU5NDI0NyJ9.dRUUQYJ-RmxgoExwPyrvHPzX9SsxcpX1rOWlhisxNsg
https://jwt.io/生成的令牌:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjbGllbnRfaWQiLCJhdWQiOiJodHRwczovL2xvZ2luLmdvb2dsZS5jb20vb2F1dGgiLCJzdWIiOiJBY2NvdW50TlVtYmVyLlFUVlIiLCJleHAiOiIxNTAxODYwMDg5IiwibmJmIjoiMTUwMTU5NDI0NyJ9.INp-ZnnL8Uj7MIwLYmpZtGyTyZG-oqZRNW8iZ145jVs
当我调用端点时,由https://jwt.io/生成的令牌有效。我得到一个状态代码 200(成功)。
但是,当我使用从我的程序生成的令牌时,它会给出“无效令牌” - 400(错误请求)。
我试图看看是否有更有效的方法来编写ifif语句.编写API以根据调用类的参数数量生成URL.
对于Ex:
def Cars(self, model=null, color=null, miles=null)
if model == null and color == null and miles ==null:
url = cars/
elif model != null and color == null and miles ==null:
url = cars/model=%s)% model
elif model != null and color != null and miles ==null:
url = cars/model=%s/color=%s)% model, color
else url = someting
return url
Run Code Online (Sandbox Code Playgroud)
我有超过10个参数,并且不想用所有组合编写那么多elif语句.