2017-08-24 71 views
0

我有权访问一个API,我试图开始利用它来自动化一些任务,然后我跳进它,但被JWT阻止,这是我从未使用过的。我也是一两年没有使用python,所以我有点生疏。请多多包涵。发送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。

谢谢!

+0

1.大多数情况下,我知道令牌端点需要一个POST(您尝试过),但上面的描述是关于GET的。你能找出更多吗? 2.带有密码的令牌请求通常如下所示:(在POST正文中)“grant_type = password&username = admin&password = 123”,所以也许你必须用uuid替换用户名? 3.一旦你获得了一个令牌,你必须像这样将令牌添加到每个请求的头部:“授权:承载者”(这是唯一的东西,这里很清楚,如果你有问题,请编辑你的问题。更多信息或取得任何进展 – jps

+0

4。获得工具提琴手,这将节省一些时间,当你试图找出你的请求的正确格式,并且只有当你把你的请求翻译成python时,你才能知道。 – jps

回答

0

问题:为了获得智威汤逊,在JSON网络令牌发送用户的API密钥(UUID)和密码使用python_jwt GET请求

解决方案。

假设
编码方法= HS256
claims字段名 'consumerId'
claims字段名 '列举HTTPMethod'

JWTurl样子:

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

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

注意:你必须使用https://<base url>/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

相关问题