2014-01-14 201 views
1

嗨我正在研究一個簡單的程序,使用REST API從路由器獲取令牌標識。我面臨的問題是,當我使用HTTPDigestAuth時,我看不到授權標題。當我使用Google App POSTMAN時,我可以看到這些標題並且可以正常工作。我的代碼中缺少什麼?Python請求摘要授權

我的代碼:

import requests 
from requests.auth import HTTPBasicAuth, HTTPDigestAuth 

user = 'pod1u1' 
passwd = 'pass' 

url = 'https://10.0.236.188/api/v1/auth/token-services' 
auth = HTTPDigestAuth(user, passwd) 
r = requests.post(url, auth=auth, verify=False) 
print 'Request headers:', r.request.headers 
print 'Status Code: ', r.status_code 
print 'response Headers: ', r.headers 

print '######################################' 

auth = HTTPBasicAuth(user, passwd) 
r = requests.post(url, auth=auth, verify=False) 
print 'Request headers:', r.request.headers 
print 'Status Code: ', r.status_code 
print 'response Headers: ', r.headers 

Shell命令瓦特/輸出:

我的腳本 -

$python digest.py 
Request headers: CaseInsensitiveDict({'Content-Length': '0', 'Accept-Encoding': 'gzip,  deflate, compress', 'Accept': '*/*', 'User-Agent': 'python-requests/2.2.0 CPython/2.7.5 Darwin/13.0.0'}) 
Status Code: 401 
response Headers: CaseInsensitiveDict({'date': 'Tue, 14 Jan 2014 00:28:27 GMT', 'content-length': '83', 'content-type': 'application/json', 'connection': 'keep-alive', 'server': 'nginx/1.4.2'}) 
###################################### 
Request headers: CaseInsensitiveDict({'Accept': '*/*', 'Content-Length': '0', 'Accept- Encoding': 'gzip, deflate, compress', 'Authorization': u'Basic cG9kMXUxOkMxc2NvTDF2Mw==', 'User-Agent': 'python-requests/2.2.0 CPython/2.7.5 Darwin/13.0.0'}) 
Status Code: 401 
response Headers: CaseInsensitiveDict({'date': 'Tue, 14 Jan 2014 00:28:27 GMT', 'content-length': '448', 'content-type': 'text/html', 'connection': 'keep-alive', 'server': 'nginx/1.4.2'}) 

郵差

POST /api/v1/auth/token-services HTTP/1.1 
Host: 10.0.236.188 
Authorization: Digest username="pod1u1", realm="[email protected]", nonce="",  uri="/api/v1/auth/token-services", response="08ac88b7f5e0533986e9fc974f132258", opaque="" 
Cache-Control: no-cache 


{ 
    "kind": "object#auth-token", 
    "expiry-time": "Tue Jan 14 00:09:27 2014", 
    "token-id": "Vj7mYUMTrsuljaiXEPoNJNiXLzf8UeDsRnEgh3DvQcU=", 
    "link": "https://10.0.236.188/api/v1/auth/token-services/9552418862" 
} 

回答

0

你正在做一個POST,所以直觀地說,你需要傳遞'params'參數requests.post方法

可以使用嗅探器來查看到底是什麼郵差發送到URL和做同樣的...

只是爲了信息,我與消化憑據requests.get(在另一個網址),它工作,我看到auth頭。

也許你可以先開始用GET來創建一個「會話」,然後做你的帖子,只是猜測:)

[加]

我也嘗試使用「原始」作爲標題解決方法:

[...] 
headers = { 
    "Host": "10.0.236.188", 
    "Authorization": '''Digest username="pod1u1", realm="[email protected]", nonce="",  uri="/api/v1/auth/token-services", response="08ac88b7f5e0533986e9fc974f132258", opaque=""''', 
    "Cache-Control": "no-cache" 
} 
r = requests.post(url, auth=auth, headers=headers, verify=False) 

[/加]

+0

這是來自POSTMAN請求的響應。有問題的路由器是思科品牌,這種格式也在那裏的文檔中詳細說明。 – selllikesybok

+0

@selllikesybok:相應地改變了我的答案......您是否嘗試先做GET或使用嗅探器? –

+0

@selllikesybok:和別的,url是https,爲什麼不使用verify = True?哼,不,這不應該改變任何東西的服務器端... –

0

的問題是在服務器端:Lukasa @ Github上幫助我。 「這看起來不像是一個需要摘要認證的服務,如果需要摘要認證,401應該包含這樣一個標頭:WWW-Authenticate:Digest qop =」auth「,但這不是。返回一個包含錯誤消息的JSON正文 Digest Auth不應該在初始消息上發送標題,因爲服務器需要通知您如何生成摘要。在我們能夠正確生成標題之前,我們需要服務器的領域,隨機數和qop。「