question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Code:

from TikTokApi import TikTokApi
api = TikTokApi()
for video in api.trending.videos():
    print(video.as_dict)

Error:

Traceback (most recent call last):
  File "d:\TikTok\TikTok ID Samples.py", line 3, in <module>
    for video in api.trending.videos():
  File "C:\Users\fun64\AppData\Local\Programs\Python\Python310\lib\site-packages\TikTokApi\api\trending.py", line 40, in videos
    ttwid = spawn.cookies["ttwid"]
  File "C:\Users\fun64\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\cookies.py", line 334, in __getitem__
    return self._find_no_duplicates(name)
  File "C:\Users\fun64\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\cookies.py", line 413, in _find_no_duplicates
    raise KeyError(f"name={name!r}, domain={domain!r}, path={path!r}")
KeyError: "name='ttwid', domain=None, path=None"

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:13
  • Comments:22

github_iconTop GitHub Comments

3reactions
Joystickplayscommented, Nov 6, 2022

Hey everyone! I managed to get a fix (hopefully it works to others). I changed C:\Users\user\AppData\Local\Programs\Python\Python310\Lib\site-packages\TikTokApi\api\search.py for this.

You change…

spawn = requests.head(
    "https://www.tiktok.com",
    proxies=Search.parent._format_proxy(processed.proxy),
    **Search.parent._requests_extra_kwargs
)
ttwid = spawn.cookies["ttwid"]

to…

spawn = requests.get(
    "https://www.tiktok.com",
    proxies=Search.parent._format_proxy(processed.proxy),
    **Search.parent._requests_extra_kwargs,
    headers={
        'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5"
    }
)
ttwid = spawn.cookies.get_dict()["ttwid"]

Basically, I changed the request type from HEAD to GET, and added a User-Agent. I compiled all the answers I found from trying to fix this error, so technically this piece of work was a team effort.

Quick notice: Some other python files still have that old code. So just replace that old code with the new one. If it still errors, try to get some common sense and maybe you can fix it.

3reactions
kamihorkcommented, Oct 16, 2022

I think I can solve this problem by logging into tiktok and importing the cookie https://github.com/davidteather/TikTok-Api/issues/891#issuecomment-1148930488

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python KeyError Exceptions and How to Handle Them
A Python KeyError exception is what is raised when you try to access a key that isn't in a dictionary ( dict )....
Read more >
keyerror in Python – How to Fix Dictionary Error
When working with dictionaries in Python, a KeyError gets raised when you try to access an item that doesn't exist in a Python...
Read more >
[BUG] KeyError: 'unix_user' with setuptools 63.2.0 #3582
It seems to me like installing a development version becomes increasingly impossible. Expected behavior. The error should not happen. How to ...
Read more >
I'm getting Key error in python - Stack Overflow
A KeyError generally means the key doesn't exist. So, are you sure the path key exists? From the official python docs: exception KeyError....
Read more >
Is this a bug? KeyError: 'is_popup' on mysite/admin, while ...
Is this a bug? KeyError: 'is_popup' on mysite/admin, while logging level = DEBUG. Django 1.10.6. 1167 views.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found