Gabriel Snikuer
Gabriel Snikuer

Reputation: 11

Python authentication does not work on HTTPS page

I am quite new to Python but I receive some error message to log into this page.

I am thinking it may have something to do with Javascript being used but I am not sure. Any ideas?

I also tried with BeautifulSoup but it seems that the data sent is not attributed to the Form for authentication.

Below is the code which i tried with.

import requests 

url = "virtelaview.net/web/guest/home" 
myobj = {'login': 'MYLOGIN', 'password': 'MYPASS'} 
x = requests.post(url, data = myobj, auth = ('login', 'password'), verify=False) 

Upvotes: 1

Views: 100

Answers (1)

Basically you are passing the wrong login url and the important point which you skipped is to maintain the session with fetching the Session id firstly and then passing it to the headers

import requests
from bs4 import BeautifulSoup
import urllib3

urllib3.disable_warnings()

headers = {
    'COOKIE_SUPPORT': 'true',
    'GUEST_LANGUAGE_ID': 'en_US',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0'
}
data = {
    '_58_redirect': '',
    'login': '[email protected]',
    'password': 'testpass'
}
url = 'https://www.virtelaview.net/web/guest/home?p_auth=&p_p_id=58&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&saveLastPath=0&_58_struts_action=/login/login'
with requests.Session() as s:
    s.get(url, headers=headers, verify=False)
    bo = dict(s.cookies)
    headers['JSESSIONID'] = bo['JSESSIONID']
    r = s.post(url, data=data, verify=False, headers=headers)
    soup = BeautifulSoup(r.text, 'html.parser')
    print(soup.prettify())

Because i used wrong login details so i got the following response:

<div class="portlet-msg-error">Authentication failed. Please try again.</div>

That's confirm I'm on the right track.

Another solution if the above code did not logged you. here i fetched the p_auth parameter value during each request because it's dynamic.

import requests
from bs4 import BeautifulSoup
import urllib3

urllib3.disable_warnings()

headers = {
    'COOKIE_SUPPORT': 'true',
    'GUEST_LANGUAGE_ID': 'en_US',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0'
}
data = {
    '_58_redirect': '',
    'login': '[email protected]',
    'password': 'testpass'
}
url = 'https://www.virtelaview.net/web/guest/home?p_auth=&p_p_id=58&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&saveLastPath=0&_58_struts_action=/login/login'
with requests.Session() as s:
    s.get(url, headers=headers, verify=False)
    bo = dict(s.cookies)
    headers['JSESSIONID'] = bo['JSESSIONID']
    r = s.post(url, data=data, verify=False, headers=headers)
    soup = BeautifulSoup(r.text, 'html.parser')
    for item in soup.findAll('form', attrs={'class': 'aui-form'}):
        login = item.get('action')
    r1 = s.post(login, data=data, verify=False, headers=headers)
    soup1 = BeautifulSoup(r1.text, 'html.parser')
    print(soup1.prettify())

Upvotes: 1

Related Questions