Python会话/请求在会话期间丢失身份验证

时间:2016-08-03 01:46:32

标签: python session authentication python-requests campaign-monitor

我正在尝试登录Campaign Monitor以从与电子邮件广告系列效果相关的网页中删除一些数据。

我尝试访问的网页的“登录保护”网址如下所示:

https://mycompany.createsend.com/campaigns/reports/lists/DFGDF987GD98F7GD?s=BCV98B5XF54BVC54BC

在网络浏览器(try it here)中转到该页面将重定向到登录页面,其中包含如下URL:

https://login.createsend.com/l/98SDF76DS87F68S/DFGDF987GD98F7GD?ReturnUrl=%2Fcampaigns%2Freports%2Flists%2FBCV98B5XF54BVC54BC%3Fs%3BCV98B5XF54BVC54BC&s=7DS6F87S6DF876SDF76

我试图解决的问题是,我需要打开一个会话,对重定向URL进行身份验证,然后请求我真正想要的URL(使用经过身份验证的会话)。

以下是我用来尝试实现的代码:

payload = {
    'username': 'myUsername',
    'password': 'myPassword',
}

redURL = 'https://login.createsend.com/l/98SDF76DS87F68S/DFGDF987GD98F7GD?ReturnUrl=%2Fcampaigns%2Freports%2Flists%2FBCV98B5XF54BVC54BC%3Fs%3BCV98B5XF54BVC54BC&s=7DS6F87S6DF876SDF76'

with requests.Session() as s:
    p = s.post(redURL, data=payload)

    # This prints the "success" message I've pasted below
    print p.content

    r = s.get('https://mycompany.createsend.com/campaigns/reports/lists/DFGDF987GD98F7GD?s=BCV98B5XF54BVC54BC')

    # This prints the HTML of the login page again, as if I'm not authenticated
    print r.content

以下是会话第一次POST后的“成功”响应:

{"MultipleAccounts":false,"LoginStatus":"Success","SiteAddress":"https://mycompany.createsend.com","ErrorMessage":"","SessionExpired":false,"Url":"https://mycompany.createsend.com/login?Origin=Marketing\u0026ReturnUrl=%2fcampaigns%2freports%2flists%2f92D2FBCV98B5XF54BVC%3fs%7DS6F87S6DF876SDF76\u0026s=2FBCV98B5XF54BVC","DomainSwitchAddress":"https://mycompany.createsend.com","DomainSwitchAddressQueryString":null,"NeedsDomainSwitch":false}

有人可以帮我解释为什么会话中的第二个请求会打印登录页面的HTML而不是页面的经过身份验证的版本的HTML(即包含我正在寻找的数据的页面)?

0 个答案:

没有答案