Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
grab session id from edX cookie
def login(email, password, base_url=''):
"""Login via HTTP and parse sessionid from the cookie."""
r = requests.get('{}/login'.format(base_url))
csrf = r.cookies['csrftoken']
payload = {'email': email, 'password': password}
cookies = {'csrftoken': csrf}
headers = {'referer': '{}/login'.format(base_url), 'X-CSRFToken': csrf}
r ='{}/user_api/v1/account/login_session/'.format(base_url),
data=payload, cookies=cookies, headers=headers)
session_id = r.cookies['prod-edx-sessionid'] # production
except KeyError:
session_id = r.cookies['sessionid'] # sandbox
if r.status_code != 200:
raise RuntimeError('failed login')
return session_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.