![](/img/trans.png)
[英]Create access token for Google Bigquery using JWT with python
[英]How to create a signed JWT token using python
我使用jwcrypto
库创建了一个签名的 JWT。 要求是生成由 RSA 密钥的私有组件签名的 JWT。 我采取了以下步骤
创建 JWK 密钥对
from jwcrypto import jwk,jwt
key = jwk.JWK.generate(
kty='RSA',
size=2048,
kid='test',
use='sig',
e='AQAB',
alg='RS256'
)
private_key = key.export_private()
public_key = key.export_public(as_dict=True)
然后我将公钥发送到服务器并像这样创建了签名的 JWT,可能做错了:
from datetime import datetime as dt
jwt_header = {
'alg':'RS256',
'kid':'test',
}
jwt_claims = {
'iss':'767676',
'sub':'test',
'aud':'https://example.com',
'token.aud': 'https://example.com',
'iat':int(dt.now().timestamp()),
'exp':int(dt.now().timestamp())+600
}
jwt_token = jwt.JWT(
header = jwt_header,
claims = jwt_claims,
)
jwt_token.make_signed_token(key)
signed_jwt = jwt_token.serialize()
将 JWT 发送到服务器:
headers = {
'Accept-Encoding':'gzip,deflate',
'Content-Type': 'application/x-www-form-urlencoded',
'Host': 'test.example.com',
'Connection': 'Keep-Alive',
'User-Agent': 'TestApp/1.0.0'
}
params = {
'grant_type':'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion':signed_jwt,
'client_id':'123456'
}
r = requests.post("https:example.com",headers=headers,params=params)
auth_data = r.json()
当我将签名的signed_jwt
传递给服务器时,我收到错误'Invalid Grant Type. Only jwt-bearer supported.
'Invalid Grant Type. Only jwt-bearer supported.
我怎样才能得到这个工作?
也很高兴使用不同库的答案
看起来更多的是与服务器交互的问题(上面的example.com
)。 您应该查看其 API 文档。
通常,令牌作为 header 发送,如下所示:
headers = {
# ...
"Authorization": "Bearer {signed_jwt}"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.