2017-08-11 231 views
0

我試圖從我的家庭筆記本電腦打電話給我的jwt驗證django api居住在EC2實例。該API將返回給我一個身份驗證密鑰,但是當我嘗試使用身份驗證密鑰時,它告訴我我沒有授權。我提供了一個我嘗試使用的各種方法來實現這個工作的例子。Python JWT授權令牌不授權與Django的jwt api

是否有任何具體的我在做這個代碼錯誤,這將保證我的代碼始終返回401錯誤?當要求使用JWT令牌進行身份驗證的API時,是否有任何特定的我需要執行的操作或者具體的方法,我需要設置我的頭文件以便我可以獲取返回的值?或者這看起來像是後端的問題?

import requests 
from urllib2 import Request, urlopen 
import jwt 
from requests_jwt import JWTAuth, payload_method 
import json 


def tryme(chats, payload, jwt): 
    #method1 
    res = requests.post(chats, json=payload) 
    print res.status_code 
    # error 401 

    # method2 
    req = requests.get(chats, params=payload) 
    print req.status_code 
    # error 401 

    # method3 
    req = Request(chats) 
    req.add_header('Authorization', 'Token token={}'.format(auth['token'])) 
    req.add_header('content-type', 'application/json') 
    res = urlopen(req) 
    print res.status_code 
    # error 401 

    #method4 
    token = JWTAuth(jwt['token']) 
    out = requests.get(chats, auth=token) 
    print out.status_code 
    # error 401 

    out = requests.post(chats, auth=token) 
    print out.status_code 
    # error 401 


def main(): 
    payload = { 
     'username': 'testuser', 
     'password': 'test1234' 
    } 
    base_url = 'https://www.example.com/api/v1/' 
    api_auth = base_url + 'api-token-auth/' 
    chats = base_url + 'chats/' 
    auth = json.loads(requests.post(api_auth, json=payload).content) 
    # returns auth token 

    payload = {'Authorization': 'Token {}'.format(auth['token']), 'content-type': 'application/json'} 
    tryme(chats, payload, auth) 
    payload = {'Authorization': 'Bearer {}'.format(auth['token']), 'content-type': 'application/json'} 
    tryme(chats, payload, auth) 
    payload = {'Authorization': 'JWT {}'.format(auth['token']), 'content-type': 'application/json'} 
    tryme(chats, payload, auth) 

if __name__ == '__main__': 
    main() 
+1

缺省頭方案是'授權:JWT '。在方法3中,您可以使用它。 –

+0

完美。我會看到關於提煉。如果它有效,我會讓你知道,你可以爲我創建一個答案 – castaway2000

回答

0

這已解決。但是我升級到python3。

import requests 
    import urllib3 
    urllib3.disable_warnings() 
    payload = { 
     'username': 'testuser', 
     'password': 'test1234' 
    } 
    base_url = 'https://www.example.com/api/v1/' 
    api_auth = base_url + 'api-token-auth/' 
    chats = base_url + 'chats/' 

    # get jwt token from login credentials 
    auth = json.loads(requests.post(api_auth, json=payload).content)['token'] 

    # correctly format the call which was my primary issue above. 
    payload = {'Authorization': 'JWT {}'.format(auth)} 

    # in python3 use urllib3. 
    http = urllib3.PoolManager() 
    res = http.request('GET', chats, headers=payload) 
    print(res.data) 

在python2:在Django JWT

import requests 
    payload = { 
     'username': 'testuser', 
     'password': 'test1234' 
    } 
    base_url = 'https://www.example.com/api/v1/' 
    api_auth = base_url + 'api-token-auth/' 
    chats = base_url + 'chats/' 

    # get jwt token from login credentials 
    auth = json.loads(requests.post(api_auth, json=payload).content)['token'] 

    # correctly format the call which was my primary issue above. 
    payload = {'Authorization': 'JWT {}'.format(auth)} 

    # make sure you are using the right request (GET vs POST) 
    out = requests.get(chats, headers=token) 
    print out.content