Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
grab session id from edX cookie
def login(email, password, base_url='https://courses.edx.org'):
"""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 = requests.post('{}/user_api/v1/account/login_session/'.format(base_url),
data=payload, cookies=cookies, headers=headers)
try:
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')
else:
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.