Python请求摘要授权

时间:2014-01-14 00:49:15

标签: python nginx python-requests

您好我正在开发一个简单的程序,使用REST API从路由器获取令牌ID。我面临的问题是,当我使用HTTPDigestAuth时,我没有看到Authorization标头。当我使用Google App POSTMAN时,我可以看到标题并且它可以正常工作。我的代码中缺少什么?

我的代码:

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命令w / output:

我的剧本 -

$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="pod1u1@ecatsrtpdmz.cisco.com", 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"
}

2 个答案:

答案 0 :(得分:0)

您正在进行POST,直观地说,您需要将'params'参数传递给requests.post方法

您可以使用嗅探器查看POSTMAN发送到网址的确切内容并执行相同操作...

只是为了获取信息,我使用摘要凭证(在另一个网址上)进行了request.get,它工作正常,我看到了auth标头。

也许你可以先用GET开始创建一个“会话”然后做你的POST,只是猜测:)

[ADDED]

我还会尝试使用“raw”标头作为解决方法:

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

[/ ADDED]

答案 1 :(得分:0)

问题出在服务器端:Lukasa @ GitHUB帮助我。 “这看起来不像需要Digest Auth的服务。如果需要Digest Auth,401应该包含这样的标题:WWW-Authenticate:Digest qop =”auth“。这不是。相反,你正在返回包含错误消息的JSON正文。 Digest Auth不应在初始消息上发送标头,因为服务器需要通知您如何生成摘要。我邀请您打开生成摘要的代码部分。在我们正确生成标题之前,我们需要来自服务器的领域,现时和qop。“