TikTok-Api icon indicating copy to clipboard operation
TikTok-Api copied to clipboard

[BUG] - Error encountered getting comments: TikTokApi.exceptions.EmptyResponseException: None -> TikTok returned an empty response

Open binaryplatitude opened this issue 1 year ago • 13 comments

Describe the bug

Tiktok throwing a fit and not giving me my comments.

The buggy code

Please add any relevant code that is giving you unexpected results.

Preferably the smallest amount of code to reproduce the issue.

from TikTokApi import TikTokApi
import asyncio
import os


video_id = 7248300636498890011
ms_token = os.environ.get("ms_token", None)  # set your own ms_token


async def get_comments():
    async with TikTokApi() as api:
        await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3)
        video = api.video(id=video_id)
        count = 0
        async for comment in video.comments(count=10):
            print(comment)
            print(comment.as_dict)


if __name__ == "__main__":
    asyncio.run(get_comments())

Expected behavior

Comments printed out

Error Trace (if any)

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

Traceback (most recent call last):
File "c:\Users\user\Documents\Python Project Workspace\Tiktok Data\commentTester.py", line 23, in
asyncio.run(get_comments())
File "C:\Users\user\anaconda3\envs\scapersecondenv\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "C:\Users\user\anaconda3\envs\scapersecondenv\lib\asyncio\base_events.py", line 649, in run_until_complete
return future.result()
File "c:\Users\user\Documents\Python Project Workspace\Tiktok Data\commentTester.py", line 17, in get_comments
async for comment in video.comments(count=10):
File "C:\Users\user\anaconda3\envs\scapersecondenv\lib\site-packages\TikTokApi\api\video.py", line 263, in comments
resp = await self.parent.make_request(
File "C:\Users\user\anaconda3\envs\scapersecondenv\lib\site-packages\TikTokApi\tiktok.py", line 430, in make_request
raise EmptyResponseException(result, "TikTok returned an empty response")
TikTokApi.exceptions.EmptyResponseException: None -> TikTok returned an empty response

Desktop (please complete the following information):

  • OS: [e.g. Windows 10]
  • TikTokApi Version [e.g. 5.0.0] - 6.2.0

Additional context

Its an older bug sir, but it checks out.

binaryplatitude avatar Dec 19 '23 22:12 binaryplatitude

Looks like api is broken, i tried manualy do request to get user info https://www.tiktok.com/api/user/detail/?uniqueId=therock&msToken=token and its really return empty response

algoprofit8 avatar Dec 20 '23 15:12 algoprofit8

I am getting this same exception on Mac Vetura 13.3 with the basic "trending videos" example.

`from TikTokApi import TikTokApi import asyncio import os

ms_token = os.environ.get("ms_token", None) # get your own ms_token from your cookies on tiktok.com

async def trending_videos(): async with TikTokApi() as api: await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3) async for video in api.trending.videos(count=30): print(video) print(video.as_dict)

if name == "main": asyncio.run(trending_videos())`

Traceback:

/Users/alexsharper/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:34: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020 warnings.warn( Traceback (most recent call last): File "/Users/alexsharper/Documents/Lancing/Etai/TikTokBot/ex1.py", line 15, in <module> asyncio.run(trending_videos()) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/runners.py", line 44, in run return loop.run_until_complete(main) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete return future.result() File "/Users/alexsharper/Documents/Lancing/Etai/TikTokBot/ex1.py", line 10, in trending_videos async for video in api.trending.videos(count=30): File "/Users/alexsharper/Library/Python/3.9/lib/python/site-packages/TikTokApi/api/trending.py", line 43, in videos resp = await Trending.parent.make_request( File "/Users/alexsharper/Library/Python/3.9/lib/python/site-packages/TikTokApi/tiktok.py", line 430, in make_request raise EmptyResponseException(result, "TikTok returned an empty response") TikTokApi.exceptions.EmptyResponseException: None -> TikTok returned an empty response

Xarbenence avatar Dec 23 '23 23:12 Xarbenence

Any updates on this? Running the basic example and I was able to get a response once but after that I get "TikTok returned an empty response"

gabrielrosendo avatar Jan 09 '24 03:01 gabrielrosendo

I have the same issue...

Xarenn avatar Jan 09 '24 22:01 Xarenn

I've tried this any it work: in the api.create_sessions you should add the param headless=False, this will open the playwright browser on your computer and then you can have the response. Full command:

api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3, headless=False)

It's a bit annoying but preventing the Empty Response error

dungdl avatar Jan 10 '24 02:01 dungdl

Nope, for comments still doesn't work

Xarenn avatar Jan 10 '24 21:01 Xarenn

I've tried this any it work: in the api.create_sessions you should add the param headless=False, this will open the playwright browser on your computer and then you can have the response. Full command:

api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3, headless=False)

It's a bit annoying but preventing the Empty Response error

This can also be fixed by setting the user_agent and viewport in the context_options while remaining headless

import asyncio
import os

ms_token = os.environ.get(
    "ms_token", None
) 
context_options = {
    'viewport' : { 'width': 1280, 'height': 1024 },
    'user_agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
}

async def trending_videos():
    async with TikTokApi() as api:
        await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3, context_options=context_options)
        async for video in api.trending.videos(count=30):
            print(video)
            print(video.as_dict)

if __name__ == "__main__":
    asyncio.run(trending_videos())

This also seems to bypass the need to have a ms_token

VitoLin avatar Jan 14 '24 16:01 VitoLin

@VitoLin This worked for me for a few runs, then it went back to the "EmptyResponseException: None -> TikTok returned an empty response" error. Any idea why it only works periodically? Is there a rate limit or something? Thanks

calvin5walters avatar Jan 22 '24 22:01 calvin5walters

Has anyone tried rotating the ms_token or proxy we go through? It might be bot/scraping-detection on TikTok's part.

jpratt9 avatar Jan 29 '24 21:01 jpratt9

Has anyone tried rotating the ms_token or proxy we go through? It might be bot/scraping-detection on TikTok's part.

What do you mean by "rotating" the ms_token? And how would one do that? Thanks! @jpratt9

calvin5walters avatar Jan 29 '24 21:01 calvin5walters

I've tried this any it work: in the api.create_sessions you should add the param headless=False, this will open the playwright browser on your computer and then you can have the response. Full command:

api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3, headless=False)

It's a bit annoying but preventing the Empty Response error

This can also be fixed by setting the user_agent and viewport in the context_options while remaining headless

import asyncio
import os

ms_token = os.environ.get(
    "ms_token", None
) 
context_options = {
    'viewport' : { 'width': 1280, 'height': 1024 },
    'user_agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
}

async def trending_videos():
    async with TikTokApi() as api:
        await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3, context_options=context_options)
        async for video in api.trending.videos(count=30):
            print(video)
            print(video.as_dict)

if __name__ == "__main__":
    asyncio.run(trending_videos())

This also seems to bypass the need to have a ms_token

This only seems to work for me with api.trending, not api.user

jpratt9 avatar Jan 30 '24 23:01 jpratt9

I'm still getting EmptyResponse no matter which one I try, even with headless=False

carol-he avatar Feb 11 '24 21:02 carol-he

Finally, I got a response. Maybe someone can help me out with headless=False. How not to show the pop-up window? Without this attribute, I got error: TikTokApi.exceptions.EmptyResponseException: None -> TikTok returned an empty response

cloudengineer89 avatar Feb 20 '24 10:02 cloudengineer89