[BUG] - Getting tiktoks by username fails by "KeyError: "name='csrf_session_id', domain=None, path=None"
See original GitHub issueHi, I try to get tiktoks info by username, it worked fine until about August 13, then it started to fail by captcha. I solved captcha issue by using proxy, but by_username() method started to fail by "KeyError: “name=‘csrf_session_id’, domain=None, path=None”. I’m using TikTokApi v4.0.1 and here is my code:
import sys
import json
import logging
from TikTokApi import TikTokApi
use_test_endpoints=False
username = sys.argv[1]
count = int(sys.argv[2])
proxy="http://ххххх"
requests_extra_kwargs={"verify": '/path/to/ca.crt'}
if len(sys.argv) > 3:
custom_verifyFp=sys.argv[3]
else:
custom_verifyFp=""
api = TikTokApi.get_instance(use_test_endpoints=use_test_endpoints, custom_verifyFp=custom_verifyFp, proxy=proxy, logging_level=logging.DEBUG, requests_extra_kwargs=requests_extra_kwargs)
tiktoks = json.dumps(api.by_username(username=username, count=count))
print(tiktoks)
And here is a response I get:
INFO:root:Class initalized
DEBUG:asyncio:Using selector: EpollSelector
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): tiktok.com:443
DEBUG:urllib3.connectionpool:https://tiktok.com:443 "GET /@znp08?lang=en HTTP/1.1" 200 None
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): m.tiktok.com:443
DEBUG:urllib3.connectionpool:https://m.tiktok.com:443 "HEAD /api/post/item_list/?aid=1988&app_name=tiktok_web&device_platform=web_mobile®ion=US&priority_region=&os=ios&referer=&root_referer=&cookie_enabled=true&screen_width=1380&screen_height=600&browser_language=en-us&browser_platform=iPhone&browser_name=Mozilla&browser_version=Mozilla%252F5.0%2B%28iPhone%253B%2BCPU%2BiPhone%2BOS%2B12_2%2Blike%2BMac%2BOS%2BX%29%2BAppleWebKit%252F605.1.15%2B%28KHTML%2C%2Blike%2BGecko%29%2BVersion%252F13.0%2BMobile%252F15E148%2BSafari%252F604.1&browser_online=true&timezone_name=Asia%252FVladivostok&is_page_visible=true&focus_state=true&is_fullscreen=false&history_len=4&language=en&count=3&id=6747250096503129093&cursor=0&type=1&secUid=MS4wLjABAAAAhOwyBs6Q6m5ASaPVv4eeb2d_-2oZmrOe_OW-EPEUL-zhnexHCyZpXgO5Lb4_oRVr&sourceType=8&appId=1233®ion=US&priority_region=US&language=en&verifyFp=my_verify_fp&device_id=2412217525666279038&_signature=_02B4Z6wo00f01mRCymQAAIBAW-pUEdFm1GJkU87AAPhE5a HTTP/1.1" 503 0
Traceback (most recent call last):
File "/var/www/app/python/scripts/TikTok/get_tiktoks_by_username.py", line 20, in <module>
tiktoks = json.dumps(api.by_username(username=username, count=count))
File "/usr/local/lib/python3.7/dist-packages/TikTokApi/tiktok.py", line 622, in by_username
**kwargs,
File "/usr/local/lib/python3.7/dist-packages/TikTokApi/tiktok.py", line 582, in user_posts
res = self.get_data(url=api_url, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/TikTokApi/tiktok.py", line 254, in get_data
csrf_session_id = h.cookies["csrf_session_id"]
File "/usr/local/lib/python3.7/dist-packages/requests/cookies.py", line 328, in __getitem__
return self._find_no_duplicates(name)
File "/usr/local/lib/python3.7/dist-packages/requests/cookies.py", line 399, in _find_no_duplicates
raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))
KeyError: "name='csrf_session_id', domain=None, path=None"
Also I’ve tried to use different custom_verifyFp but it makes no sense.
Any help would be appreciated, thanks in advance!
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Cannot login to website using requests module (Python ...
First of all you catch KeyError exception, this mean that cookies dictionary have no key csrftoken . So you need explore your response...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Please try using V5.0.0 this may solve your problem
I had this error too, added debugs and:
I had exactly 3 such errors when I called x2363
user_posts
(each was valid uid, secUID, always page_size=30). PS. It looks likex-secsdk-csrf-token
is an optional header. I removed it and it works for me, but I’m using only JSON API (onlyuser_page
, without anyget_user_object
, I cached uid and secUID). it sent a random string earlier: https://github.com/davidteather/TikTok-Api/commit/e70849477f5e27b4760d0a5d610834bdeeb60f6d