Niek de Klein
Niek de Klein

Reputation: 8834

Authentication failure logging into website

I am trying to log into this website: https://portals.broadinstitute.org/cmap/index.jsp using code from some time back from this answer: https://stackoverflow.com/a/22547541/651779. This used to work correctly. However, when running it now I get an authorization denied. The username and password are correct.

Below the code:

login_url = 'http://www.broadinstitute.org/cmap/j_security_check'

login_values = urllib.parse.urlencode({'j_username': user,
                                       'j_password': pwd, 
                                       'submit': 'sign in'})
print(login_url+'?'+login_values)

This gives the url (with username and password filled in):

http://www.broadinstitute.org/cmap/j_security_check?j_username=<username>&j_password=<password>&submit=sign+in

When I paste that in my browser it correctly logs me in, so I know the username and password are still good.

payload_submit_signature = bytes(login_values, 'ascii')

cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(
    urllib.request.HTTPRedirectHandler(),
    urllib.request.HTTPHandler(debuglevel=0),
    urllib.request.HTTPSHandler(debuglevel=0),
    urllib.request.HTTPCookieProcessor(cj))

opener.open(submit_signature_url) #First call to capture the JSESSIONID
resp = opener.open(login_url, payload_submit_signature)
for cookie in cj:
    print (cookie.name, cookie.value, cookie.domain, cookie.expires, cookie.secure) #etc etc

As per the previous stack answer a cookie needs to be set first. The print above gives

JSESSIONID B2CFD19CBB9105EC2DC4D75DD7C3C0C9 portals.broadinstitute.org None True

so that seems to work.

print(resp.read())

This gives the same hmtl page you get when filling in the wrong user-password combination. How come it's not sending these correctly?

Upvotes: 0

Views: 343

Answers (1)

t.m.adam
t.m.adam

Reputation: 15376

Using requests , this code works for me

login = 'https://portals.broadinstitute.org/cmap/j_security_check'
index = 'https://portals.broadinstitute.org/cmap/index.jsp'
username = 'my username'
password = 'my password'
data = { 'j_username':username, 'j_password':password, 'submit':'sign+in' }

ses = requests.Session()
ses.get(index)    # you need this to get cookies #
post = ses.post(login, data=data)

print(ses.cookies.get_dict())
print(post.url)
print('sign out' in post.content)

Result :

{'JSESSIONID': 'A86EDB10DB2BC063EFE7A528F5E88A77'}
https://portals.broadinstitute.org/cmap/index.jsp
True

Upvotes: 1

Related Questions