TikTok-Api: [BUG] - Code doesn't return anything.

I don’t get any output from any code, even the examples provided with the package and in the readme

import logging
from TikTokApi import TikTokApi

with TikTokApi(logging_level=logging.INFO) as api:
    for trending_video in api.trending.videos(count=50):
        print(trending_video.author.username)

I’ve tried reinstalling it, installing it from outside the venv, reinstalling playwright, passing the cookie to the API and nothing changed. Is anyone else having the same issue?

About this issue

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

Most upvoted comments

I was having this issue, but I noticed the url generated worked fine in my browser

So I exported the cookies from my browser (I’m logged into TikTok) into cookies.json and then was able to use the below to set those cookies in all requests through the api

import json

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


cookies = get_cookies_from_file()


def get_cookies(**kwargs):
    return cookies


api = TikTokApi()

api._get_cookies = get_cookies  # This fixes issues the api was having

This is working for me

Here’s the step-by-step that worked for me, @Wathfea:

  1. Install this cookie manager extension for Chrome (others may work): https://chrome.google.com/webstore/detail/cookiemanager-cookie-edit/
  2. Log into TikTok using Chrome
  3. Using the cookie manager extension, view all cookies from tiktok.
  4. Select and export all as a cookies.json file. These will have the keys “name” and “value”.
  5. Use the code provided by @adamd01

@Wathfea - I’m using “edit this cookie” chrome plugin to do the export of cookies

My exported file looks like this:

[
{
    "domain": ".tiktok.com",
    "expirationDate": 1686154900,
    "hostOnly": False,
    "httpOnly": False,
    "name": "_abck",
    "path": "/",
    "sameSite": "unspecified",
    "secure": True,
    "session": False,
    "storeId": "0",
    "value": "XXX",
    "id": 1
},
...(above format repeated for each cookie)

Although the only thing we need is the name and value fields, as long as those are populated the code will work And in my code you can see I actually had to format it to be a more simple format before passing it through, so eventually you just have

cookies = {'name_of_cookie': 'value', ...}

@dhudsmith or @adamd01 can you please describe step by step what did you do? I tried to recreate what you sad but I’m out of luck.

I saved the cookie.json in the above format what you wrote and chaged the code for the correct key value pairs bt the script still idle and I can’t get back any response.

Here is my code:

import json

def get_cookies_from_file():
    with open('/Users/davidperlusz/code/tiktok-scraper/cookies.json') as f:
        cookies = json.load(f)

    cookies_kv = {}
    for cookie in cookies:
        cookies_kv[cookie['Name raw']] = cookie['Content raw"']

    return cookies_kv


cookies = get_cookies_from_file()


def get_cookies(**kwargs):
    return cookies

from TikTokApi import TikTokApi

api = TikTokApi()

api._get_cookies = get_cookies  # This fixes issues the api was having

for trending_video in api.trending.videos(count=50):
    print(trending_video.author.username)

UPDATE: I was able to get @adam01’s solution to work. I was able to get the cookies.json in the correct form by using Chrome along with the Cookie Editor extension (https://add0n.com/cookie-editor.html)

@adamd01, thanks for sharing this. ~I can’t get it to work.~ Can you give a little more detail about which cookies file you saved? I’m seeing 33 cookies for the tiktok domain. I exported all of these as a cookies.json. I get an array of the following objects. Where are the “key” and “value” coming from in your snippet.

{
	"Host raw": "https://.tiktok.com/",
	"Name raw": "tt_csrf_token",
	"Path raw": "/",
	"Content raw": "<redacted>",
	"Expires": "At the end of the session",
	"Expires raw": "0",
	"Send for": "Encrypted connections only",
	"Send for raw": "true",
	"HTTP only raw": "true",
	"SameSite raw": "lax",
	"This domain only": "Valid for subdomains",
	"This domain only raw": "false",
	"Store raw": "firefox-default",
	"First Party Domain": ""
}

FYI, I’m on firefox and I am using the “Quick Cookie Manager” to export the json.

api.user is also working when you pass the output of trending_video.author to user as follows:

user = trending_video.author
print(user.info_full())

This works!!

api.trending – works only api.user – not working api.sound – not working