【问题标题】:Sending JWT Get Request containing username/password using python使用python发送包含用户名/密码的JWT Get请求
【发布时间】:2018-02-02 16:20:23
【问题描述】:

我可以访问一个 API,我试图开始利用该 API 来自动执行一些任务,我直接进入了它,但被我从未使用过的 JWT 阻碍了。我也有几年不使用python了,所以我有点生疏了。请多多包涵。

以下是 API 文档的直接引用:

The authentication mode for an organization is with a JSON Web Token. Users 
must pass a JSON Web Token (JWT) in the header of each API request made. 

To obtain the JWT, send the user’s API key (UUID) and password in a JSON Web
Token GET Request. The authorization method of “Bearer” and a 
space is then prefixed to the encoded token string returned. The token will 
be tied to the user account that generated the JWT.

我已尝试使用 requests,但出现 405 错误,我还安装并导入了 pyjwt,但这让我感到困惑。这基本上就是我试图通过 python 发送的内容:

POST https://<our endpoint>/v1/token/get HTTP/1.1
Content-Type: application/json
{
"username": "<myUsername>",
"password": "<myPassword>"

我已验证目标 API 正在运行,因为有一小部分功能无需 JWT 即可运行,并且可以通过 requests 轻松访问

欢迎提供建议,也欢迎任何教程。我尝试阅读几个 JWT 教程,但很难将其翻译成 python。

谢谢!

【问题讨论】:

  • 1.我知道令牌端点的大多数情况都需要 POST(您尝试过),但您上面的描述是关于 GET 的。你能了解更多吗? 2. 带有密码的令牌请求通常看起来像这样:(在 POST 正文中)“grant_type=password&username=admin&password=123”,所以也许你必须用 uuid 替换用户名? 3.一旦你获得了一个令牌,你必须将这样的令牌添加到每个请求的标题中:“Authorization: Bearer ”(这是唯一非常清楚的事情。请编辑你的问题,如果您有更多信息或取得任何进展。
  • 4.获取工具提琴手,当您尝试找出请求的正确格式时,这将节省一些时间,并且只有当您全部弄清楚时,您才能将您的请求转换为 python。

标签: python jwt


【解决方案1】:

问题:要获取 JWT,请在 JSON Web Token GET 请求中发送用户的 API 密钥 (UUID) 和密码

使用python_jwt 的解决方案。

假设
编码方式 = HS256
claims Fieldname 'consumerId'
claims Fieldname 'httpMethod'

url 中的 JWT 看起来像:

'http://httpbin.org/get?eyJ0eXAiOiAiSldUIiwgImFsZyI6ICJIUzI1NiJ9... (omitted for brevity)  

response.json() 包含请求的JWT,您必须在之后使用。

注意:您必须使用https://&lt;base url&gt;/v1/token/get

import python_jwt as jwt
# Create claims dictionary for generation of JwToken
claims = {
    'consumerId': 'My App ID',
    'httpMethod': 'GET'
}

import datetime
# create JWToken
jwtoken = jwt.generate_jwt(claims, 'My secret', 'HS256', datetime.timedelta(minutes=5))

response = requests.get('http://httpbin.org/get', jwtoken)
print(response.json())

使用 Python:3.4.2 测试 - 请求:2.11.1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 2020-03-28
    • 2013-11-16
    • 2012-02-20
    • 1970-01-01
    • 2018-04-24
    相关资源
    最近更新 更多