【发布时间】:2011-07-07 03:34:21
【问题描述】:
这是我在这个帐户上的第一篇文章,上周我一直在努力让它发挥作用,所以我希望有人能帮助我让它发挥作用。
我试图从https://api.connect2field.com/ 中提取一些数据,但它拒绝了我所有来自 python 的身份验证尝试(虽然不是来自浏览器)。 我使用的代码
import urllib.request as url
import urllib.error as urlerror
urlp = 'https://api.connect2field.com/api/Login.aspx'
# Create an OpenerDirector with support for Basic HTTP Authentication...
auth_handler = url.HTTPBasicAuthHandler()
auth_handler.add_password(realm='Connect2Field API',
uri=urlp,
user='*****',
passwd='*****')
opener = url.build_opener(auth_handler)
# ...and install it globally so it can be used with urlopen.
url.install_opener(opener)
try:
f = url.urlopen(urlp)
print (f.read())
except urlerror.HTTPError as e:
if hasattr(e, 'code'):
if e.code != 401:
print ('We got another error')
print (e.code)
else:
print (e.headers)
我很确定代码做的一切都是正确的,这让我觉得也许还有 ASP.net 需要的另一个身份验证步骤。有人对 ASP.Net 的身份验证协议有任何经验吗?
我会整天检查这个帖子,所以如果需要,我可以发布更多信息。
编辑:我还尝试在家里运行的基本 http auth 服务器上运行我的脚本,它会进行身份验证,所以我很确定请求设置正确。
【问题讨论】:
-
你能给出你得到的错误吗?特别是发回的标头?另外,您是否尝试使用 Wireshark 检查真正发送了哪些标头?
-
我已经向 python 提交了一个错误请求。事实证明,如果在 auth 请求标头中没有引用领域,python 会默默地失败。修复包括将 'realm=(["\'])(.*?)\\2', re.I) 更改为 'realm=(["\']?)(["\']*)\\ 2', re.I) 在 AbstractBasicAuthHandler
标签: asp.net python-3.x basic-authentication