2014-01-14 204 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。“