使用 urllib/urllib2 获取会话 cookie 并使用它登录到最终页面

Using urllib/urllib2 get a session cookie and use it to login to a final page

我需要使用 urllib/urllib2 库登录到第一个网站以检索会话 cookie,这将使我能够登录到正确的最终网站。使用 requests 库非常简单(我这样做是为了确保我可以实际访问该网站):

import requests
payload = {"userName": "username", "password": "password", "apiKey": "myApiKey"}
url = "https://sso.somewebsite.com/api/authenticateme"
session = requests.session()
r = session.post(url, payload)
# Now that I have a cookie I can actually access my final website
r2 = session.get("https://websiteineed.somewebsite.com")

我尝试使用 urllib/urllib2 库复制此行为,但不断得到 HTTP Error 403: Forbidden:

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
values = {"userId": username , "password": password, "apiKey": apiKey}
url = 'https://sso.somewebsite.com/api/authenticateme'
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
resp = urllib2.urlopen(req)
req2 = urllib2.Request('https://download.somewebsite.com')
resp2 = urllib2.urlopen(req2)

我尝试了找到 here and here and here 的解决方案,但其中 none 对我有用...如果有任何建议,我将不胜感激!

'final page' 拒绝 cookie 的原因是 Python 将 'User-agent', 'Python-urllib/2.7' 添加到 header。删除此元素后,我能够登录网站:

opener.addheaders.pop(0)