Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Daretary on March 30, 2023, 04:14:14 PM

Title: Authorization to SMF 2.1 using Python
Post by: Daretary on March 30, 2023, 04:14:14 PM
In this topic (https://www.simplemachines.org/community/index.php?topic=585067.0), you can see that SMF 1.1.21 authorizes fine through requests in Python.

I have to use Selenium with SMF 2.1. But in this way it takes a long time to load a long message into a topic.

What are the reasons why 2.1 refuses to work with requests? And how to fix it?

Your session timed out while posting. Please go back and try again.
I tried to fill in hidden fields, but to no avail.
Title: Re: Authorization to SMF 2.1 using Python
Post by: Kindred on March 30, 2023, 04:18:09 PM
Wow, that's an open ended question with no details...

If you actually want help, then providing things like, well  maybe the script which you claim used to work?

I mean, the password system and encryption changed between versions. Very little written for 1.1.x will work on 2.1.x without modification
Title: Re: Authorization to SMF 2.1 using Python
Post by: Arantor on March 30, 2023, 05:30:15 PM
Well, that and the fact that 2.0 has session security that 1.x didn't so that external stuff needs changing...
Title: Re: Authorization to SMF 2.1 using Python
Post by: Daretary on March 31, 2023, 03:24:57 AM
Kindred , sorry for not showing the code earlier.

This worked fine on SMF 1.1.21:

payload = {
    'user': 'login',
    'passwrd': 'password',
    'cookielength': '-1'
}
s = requests.Session()
p = s.post('http://site.com/index.php?action=login2', data=payload, timeout=10)

cookies = p.headers['smf_sc']
n_cookies = {}
m = re.compile('(\w+)=([a-z%0-9A-Z]+)[^;]*')
for (name, content) in re.findall(m, cookies):
    if name != "expires":
        n_cookies[name] = content

# create text content for form_data

r = s.post('http://site.com/index.php?action=post2;board=111.0', data=form_data,
           cookies=n_cookies)

I tried collecting hidden fields on action=login and then using them on action=login2. But nothing works.
Title: Re: Authorization to SMF 2.1 using Python
Post by: Arantor on March 31, 2023, 01:14:48 PM
You need those hidden fields, and to supply the cookie with them as well, since the cookie contains the session ID that verifies if the other (multiple) hidden fields are correct.
Title: Re: Authorization to SMF 2.1 using Python
Post by: Daretary on December 27, 2023, 08:58:59 AM
Finally set up creating topics using requests. Threw away slow Selenium. :)