1

I am trying to use python requests to login to my university's CAS. I've seen a few tutorials and posts online, but they all work for a CAS that has it's username and password fields on the same page. Here is the website I'm trying to login to: https://cas.tamu.edu/cas/login?service=https://howdy.tamu.edu/uPortal/Login&renew=true

As you can see, there is only a field for a username, then after clicking submit, you are taken to the same page, only the field is now for a password. Here is the current code I have that doesn't work:

import requests
import lxml.html
from bs4 import BeautifulSoup

# URL of webpage
login_url = "https://cas.tamu.edu/cas/login?service=https://howdy.tamu.edu/uPortal/Login&renew=true"
howdy = "https://howdy.tamu.edu/uPortal/normal/render.uP"
username = # my username
password = # my password

# create a session to store cookies
sesh = requests.session()

params = {'service': howdy}
# gets the URL and converts the text of the HTML code
req = sesh.get(login_url, params=params)
html_content = req.text

print html_content

# parsing the page for hidden inputs
login_html = lxml.html.fromstring(html_content)
hidden_inputs = login_html.xpath(r'//form//input[@type="hidden"]')
user_form = {x.attrib["name"]: x.attrib["value"] for x in hidden_inputs}
print(user_form)

user_form["username"] = username
user_response = sesh.post(login_url, data=user_form)

print user_response.url

# same thing for the password page
pass_form = {x.attrib["name"]: x.attrib["value"] for x in hidden_inputs}
print(pass_form)

pass_form["password"] = password
pass_response = sesh.post(user_response.url, data=pass_form)

print pass_response.url

I based this off of this tutorial: https://brennan.io/2016/03/02/logging-in-with-requests/. Specifically the section about CAS.

SlupSax
  • 61
  • 6

1 Answers1

2

I found a workaround that is not exactly what I wanted, but works for my purposes. I'm posting this in case anyone finds this with a similar problem. I used browsercookie to use the current cookies from chrome, so if I'm already logged in, then I can access info behind the CAS. More info in this post: https://stackoverflow.com/a/29628188/9613156.

SlupSax
  • 61
  • 6