HTTP 基本身份验证不适用于 Python 3

HTTP Basic Authentication not working with Python 3

我正在尝试访问启用了 HTTP 基本身份验证的 Intranet 站点。

这是我使用的代码:

from bs4 import BeautifulSoup
import urllib.request, base64, urllib.error

request = urllib.request.Request(url)
string = '%s:%s' % ('username','password')

base64string = base64.standard_b64encode(string.encode('utf-8'))

request.add_header("Authorization", "Basic %s" % base64string)
try:
    u = urllib.request.urlopen(request)
except urllib.error.HTTPError as e:
    print(e)
    print(e.headers)

soup = BeautifulSoup(u.read(), 'html.parser')

print(soup.prettify())

但是它不起作用并且失败 401 Authorization required. 我不明白为什么它不起作用。

UTF-8 编码可能不起作用。您可以尝试改用 ASCII 或 ISO-8859-1 编码。

此外,请尝试使用 Web 浏览器访问 Intranet 站点并检查授权 header 与您生成的授权有何不同。

给定的解决方案 here 无需任何修改即可使用。

from bs4 import BeautifulSoup
import urllib.request

# create a password manager
password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()

# Add the username and password.
# If we knew the realm, we could use it instead of None.
top_level_url = "http://example.com/foo/"
password_mgr.add_password(None, top_level_url, username, password)

handler = urllib.request.HTTPBasicAuthHandler(password_mgr)

# create "opener" (OpenerDirector instance)
opener = urllib.request.build_opener(handler)

# use the opener to fetch a URL
u = opener.open(url)

soup = BeautifulSoup(u.read(), 'html.parser')

前面的代码同样有效。您只需要解码 utf-8 编码的字符串,否则 header 包含 byte-sequence.

from bs4 import BeautifulSoup
import urllib.request, base64, urllib.error

request = urllib.request.Request(url)
string = '%s:%s' % ('username','password')

base64string = base64.standard_b64encode(string.encode('utf-8'))

request.add_header("Authorization", "Basic %s" % base64string.decode('utf-8'))
try:
    u = urllib.request.urlopen(request)
except urllib.error.HTTPError as e:
    print(e)
    print(e.headers)

soup = BeautifulSoup(u.read(), 'html.parser')

print(soup.prettify())

使用“ascii”编码。这对我有用。

import base64
import urllib.request

url = "http://someurl/path"
username = "someuser"
token = "239487svksjdf08234"

request = urllib.request.Request(url)
base64string = base64.b64encode((username + ":" + token).encode("ascii"))
request.add_header("Authorization", "Basic {}".format(base64string.decode("ascii")))
response = urllib.request.urlopen(request)

response.read() # final response string