0

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 = '[email protected]'
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.

1 Answer 1

1

I gave this one a go on a public demo instance and I can see the data on the Magento 2 dashboard just fine:

    import requests
    from bs4 import BeautifulSoup
    
    web_user = '[email protected]'
    web_pass = 'yourpassword'
    
    s = requests.session()
    main_url = 'https://magento2demo/'
    html_data = s.get(main_url)
    form_soup = BeautifulSoup(html_data.content, 'html.parser')
    form_key = form_soup.find('input', {'name':'form_key'})['value']
    
    login_route = 'https://magento2demo/customer/account/loginPost/'
    login_payload = {
      'form_key': form_key,
      'login[username]': web_user,
      'login[password]': web_pass
    }
    
    login_req = s.post(login_route, data=login_payload)
    
    account_url = "https://magento2demo/customer/account/"
    html_account = s.get(account_url)
    account_soup =  BeautifulSoup(html_account.content, 'html.parser')
    
    info = account_soup.find('div', {'class':'box-information'}).find('div', {'class':'box-content'})
    assert web_user in str(info)
                "beautifulsoup4": { "version": "==4.9.3"
                "requests": { "version": "==2.26.0"

What's the response code on the POST? Anything peculiar in your headers? Might wanna add more reproducible data if the above doesn't help.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your response but how can i do if I want to automate the password change for an account ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.