Hello,
I hope I'm posting my question in the right section.
I'm trying to login to Magento account from Python script using requests module, the relevant code I made looks as below:
s = requests.session() main_url = '<redacted.tld>/en/index.html'
html_data = s.get('https://'+main_url, headers=headers, timeout=(30, 30), verify=dst_verify_ssl) web_user = 'test@test.com' web_pass = '123test321' form_key = soup.find('input', {'name':'form_key'})['value'] l_url = 'https://<redacted.tld>/'
l_route = 'en/customer/account/loginPost/'
login_payload = { 'form_key':form_key, 'login[username]':web_user, 'login[password]':web_pass } login_req = s.post(l_url + l_route, headers=headers, data=login_payload)
But it's not getting me logged in so I was wondering if someone could tell me what does it take to login via Python to the Magento account?
Thanks upfront.
Hi,
please try to use Token-based authentication instead of sending requests to loginPost controller. This should be helpful for you:
https://devdocs.magento.com/guides/v2.3/get-started/authentication/gs-authentication-token.html
Regards,
Marcin
Token-based authentication isn't available for me, therefore, the only way would be to login via a POST request.
I'm not sure what info needs to be submitted in the POST request and I've found no documentation so far, could you please point me into the right direction?
I'm either missing some header when sending POST to Magento or...., I've included about everything Chrome DOM inspector captured.
I didn't found related documentation either, for instance, I tried to authenticate via POST to WordPress, took just a few minutes.
There must be something I'm missing with Magento but I ran out of ideas.
Could you solve this finally? I'm stuck at the same point..
Your code looks good overall, but there are a few things you can try to troubleshoot why you're not getting logged in:
If you're still having trouble logging in, you can try using a different HTTP client library, such as urllib3. Here is an example of a simple login script using urllib3:
import urllib3 # Set the Magento login URL login_url = 'https://<redacted.tld>/en/customer/account/loginPost/' # Set the Magento login credentials web_user = 'test@test.com' web_pass = '123test321' # Create a urllib3 client http = urllib3.PoolManager() # Create the login payload login_payload = { 'form_key': '<form_key>', 'login[username]': web_user, 'login[password]': web_pass } # Make the login request response = http.request('POST', login_url, body=login_payload) # Check the response status code if response.status == 200: print('Login successful!') else: print('Login failed: {}'.format(response.status_code))
If you're still having trouble logging in, you can contact Magento support for assistance.