cancel
Showing results for 
Search instead for 
Did you mean: 

Logging in to Magento account from Python script.

Logging in to Magento account from Python script.

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.

 

 

 

5 REPLIES 5

Re: Logging in to Magento account from Python script.

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

Re: Logging in to Magento account from Python script.

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?

Re: Logging in to Magento account from Python script.

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.

Re: Logging in to Magento account from Python script.

Could you solve this finally? I'm stuck at the same point..

Re: Logging in to Magento account from Python script.

Your code looks good overall, but there are a few things you can try to troubleshoot why you're not getting logged in:

  • Make sure that the form_key you're using is valid. You can check this by logging into your Magento account in a web browser and inspecting the source code of the login page. The form_key is the value of the input element with the name form_key.
  • Make sure that the login_url and login_route variables are correct. You can check this by logging into your Magento account in a web browser and inspecting the URL of the login page. The login_url is the base URL of your Magento installation, and the login_route is the path to the login controller.
  • Try adding a debug=True parameter to the login_req.post() call. This will print the response from the Magento server to the console, which can help you to identify any errors.

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.