Skip to content

Commit b0997c0

Browse files
authored
Merge pull request #9 from prius/dev-auth-csrf
Added auth module with csrf cookie resolver
2 parents b1b3395 + 101f83e commit b0997c0

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ Then in python shell initialize the client (if you're using chrome, cookies can
2727
import leetcode
2828

2929
# Get the next two values from your browser cookies
30-
csrf_token = "xxx"
3130
leetcode_session = "yyy"
31+
csrf_token = "xxx"
32+
33+
# Experimental: Or CSRF token can be obtained automatically
34+
import leetcode.auth
35+
csrf_token = leetcode.auth.get_csrf_cookie(leetcode_session)
3236

3337
configuration = leetcode.Configuration()
3438

leetcode/auth.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import requests
2+
3+
4+
def get_csrf_cookie(session_id: str) -> str:
5+
response = requests.get(
6+
"https://leetcode.com/",
7+
cookies={
8+
"LEETCODE_SESSION": session_id,
9+
},
10+
)
11+
12+
return response.cookies["csrftoken"]

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ six >= 1.10
33
python_dateutil >= 2.5.3
44
setuptools >= 21.0.0
55
urllib3 >= 1.15.1
6+
requests

0 commit comments

Comments
 (0)