TikTok-Api: [BUG] - Your Error Here

Read Below!!! If this doesn’t fix your issue delete these two lines

You may need to install chromedriver for your machine globally. Download it here and add it to your path.

Describe the bug Getting an error in call get_trending function

A clear and concise description of what the bug is. Tried to execute get_trending.py in the example folder by getting an error. I also tried to input a custom_verifyfp but still getting the same error.

The buggy code

get_trending.py

Please insert the code that is throwing errors or is giving you weird unexpected results.

  1. script I tried to execute is get_trending.py.
  2. second script is below, which uses the customverifyFp.

from TikTokApi import TikTokApi api = TikTokApi.get_instance() results = 10

trending = api.by_trending(count=results, custom_verifyFp=“verify_kyat3dlk_jjAEDYEw_aHce_4kZD_9uu3_J9WvCO7ktDtA”)

for tiktok in trending: # Prints the id of the tiktok print(tiktok[‘id’])

print(len(trending))

# Code Goes Here

Expected behavior Able to pull the list of trending videos

A clear and concise description of what you expected to happen. Able to pull the list of trending videos

Error Trace (if any)

Put the error trace below if there’s any error thrown.

# Error Trace Here

File “C:\Projects\tiktok\test.py”, line 7, in <module> trending = api.by_trending(count=results, custom_verifyFp=“verify_kyat3dlk_jjAEDYEw_aHce_4kZD_9uu3_J9WvCO7ktDtA”) File “C:\Projects\tiktok\env\lib\site-packages\TikTokApi\tiktok.py”, line 486, in by_trending ttwid = spawn.cookies[“ttwid”] File “C:\Projects\tiktok\env\lib\site-packages\requests\cookies.py”, line 328, in getitem return self._find_no_duplicates(name) File “C:\Projects\tiktok\env\lib\site-packages\requests\cookies.py”, line 399, in _find_no_duplicates raise KeyError(‘name=%r, domain=%r, path=%r’ % (name, domain, path)) KeyError: “name=‘ttwid’, domain=None, path=None”

Desktop (please complete the following information):

  • OS: Windows 10
  • TikTokApi Version : 4.1.0

Additional context

Add any other context about the problem here. Haven’t download chromedriver, is it really required?

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (1 by maintainers)

Most upvoted comments

Please try using V5.0.0 this may solve your problem

Looks like tiktok doesn’t generate ttwid cookie via HEAD request anymore. https://github.com/davidteather/TikTok-Api/blob/a356324ab3171d473619fdf3e1dd71e5996cfc29/TikTokApi/tiktok.py#L481

Replace request.head to request.get at 481 line of tiktok.py https://github.com/davidteather/TikTok-Api/blob/a356324ab3171d473619fdf3e1dd71e5996cfc29/TikTokApi/tiktok.py#L481

OR

You can change line 486 in tiktok.py. You will need to replace ttwid = spawn.cookies["ttwid"] for ttwid = YOURCOOKIE where is YOURCOOKIE is your ttwid cookie which you can get by navigating to tiktok.com from your browser (or via GET request). https://github.com/davidteather/TikTok-Api/blob/a356324ab3171d473619fdf3e1dd71e5996cfc29/TikTokApi/tiktok.py#L486

Code

from TikTokApi import TikTokApi

api = TikTokApi(custom_verify_fp="***")

for video in api.trending.videos():
    print(video)

Error

  File "D:\Project\Python\tiktok\main.py", line 7, in <module>
    for video in api.trending.videos():
  File "D:\Project\Python\tiktok\venv\lib\site-packages\TikTokApi\api\trending.py", line 40, in videos
    ttwid = spawn.cookies["ttwid"]
  File "D:\Project\Python\tiktok\venv\lib\site-packages\requests\cookies.py", line 328, in __getitem__
    return self._find_no_duplicates(name)
  File "D:\Project\Python\tiktok\venv\lib\site-packages\requests\cookies.py", line 399, in _find_no_duplicates
    raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))
KeyError: "name='ttwid', domain=None, path=None"

I use V5.0.0

Please open this issue. It’s still persistent and none of the suggestions above have solved it for me.

import json
from TikTokApi import TikTokApi

def get_cookies_from_file():
    with open('cookies.json') as f:
        cookies = json.load(f)
    cookies_kv = {}
    for cookie in cookies:
        cookies_kv[cookie['name']] = cookie['value']
    return cookies_kv

def get_cookies(**kwargs):
    return get_cookies_from_file()

api = TikTokApi(custom_verify_fp=get_cookies)
api._get_cookies = get_cookies  # This fixes issues the api was having

for v in api.trending.videos():
    print(v.author.username)

I’ve also hardcoded the ttwid where applicable.

For me upgrading to v5 actually introduced this bug

I experienced that too. And solution from @vionde has worked! @vionde Thank you very much