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

User Example return "TikTok returned an empty response" for user.videos

Open arhen opened this issue 1 year ago • 41 comments

Describe the bug

Like the title, if you runt the code example "user_example" you will get the `TikTokApi.exceptions.EmptyResponseException: None -> TikTok returned an empty response.

The buggy code in example.user_example.py, this below code related to the error.

async for video in user.videos(count=1):

It means, user.info is working as expected but the videos from the user is not.

SET LOGGING LEVEL TO INFO BEFORE POSTING CODE OUTPUT

{'extra': {'fatal_item_ids': [], 'logid': '2024080214523147F5B8C5A111761DB836', 'now': 1722610353000}, 'log_pb': {'impr_id': '2024080214523147F5B8C5A111761DB836'}, 'shareMeta': {'desc': '@clorismen 1.2m Followers, 5 Following, 37.2m Likes - Watch awesome short videos created by clorismen', 'title': 'clorismen on TikTok'}, 'statusCode': 0, 'status_code': 0, 'status_msg': '', 'userInfo': {'stats': {'diggCount': 0, 'followerCount': 1200000, 'followingCount': 5, 'friendCount': 5, 'heart': 37200000, 'heartCount': 37200000, 'videoCount': 4641}, 'user': {'avatarLarger': 'https://p16-sign-sg.tiktokcdn.com/aweme/1080x1080/tos-alisg-avt-0068/7df0afc38214409ecf34a9ea8e1c1df7.jpeg?lk3s=a5d48078&nonce=10688&refresh_token=79dddde3efce933597f66d60bc4ca5d3&x-expires=1722780000&x-signature=UsQbGx2TChrDWTQUZo4NHgNgAws%3D&shp=a5d48078&shcp=81f88b70', 'avatarMedium': 'https://p16-sign-sg.tiktokcdn.com/aweme/720x720/tos-alisg-avt-0068/7df0afc38214409ecf34a9ea8e1c1df7.jpeg?lk3s=a5d48078&nonce=77484&refresh_token=cd1403b79052ab6695f59f6e69a418dc&x-expires=1722780000&x-signature=8QohfoD13HeCT3aLfWcYAvIOj4A%3D&shp=a5d48078&shcp=81f88b70', 'avatarThumb': 'https://p16-sign-sg.tiktokcdn.com/aweme/100x100/tos-alisg-avt-0068/7df0afc38214409ecf34a9ea8e1c1df7.jpeg?lk3s=a5d48078&nonce=23493&refresh_token=f82e62cc12cd07990276a0e408d446f8&x-expires=1722780000&x-signature=uQDwoas0Xn0nWWFyNdzlq4XK6XY%3D&shp=a5d48078&shcp=81f88b70', 'canExpPlaylist': True, 'commentSetting': 0, 'commerceUserInfo': {'category': 'Beauty', 'categoryButton': False, 'commerceUser': True, 'downLoadLink': {'android': '', 'ios': ''}}, 'downloadSetting': 0, 'duetSetting': 0, 'followingVisibility': 1, 'ftc': False, 'id': '6842605723350172674', 'isADVirtual': False, 'isEmbedBanned': False, 'nickNameModifyTime': 1621911628, 'nickname': 'clorismen', 'openFavorite': False, 'privateAccount': False, 'profileEmbedPermission': 1, 'profileTab': {'showPlayListTab': True}, 'relation': 0, 'secUid': 'MS4wLjABAAAAde6jKQ1Oevu0_K_y0qRzxGNdSVixJ6mAy-lhI7ioL57mJ0wQ9RkG2UstegWwcIAJ', 'secret': False, 'signature': "The 1st Men's Grooming and Skincare Brand has Existed in Digital Era since 2016", 'stitchSetting': 0, 'ttSeller': True, 'uniqueId': 'clorismen', 'verified': True}}}
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/arhen/Works/test/src/tiktok-public-api-python/TikTok-Api/examples/user_example.py", line 27, in <module>
    asyncio.run(user_example())
  File "/opt/homebrew/Cellar/[email protected]/3.11.9/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.11.9/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.11.9/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/arhen/Works/test/src/tiktok-public-api-python/TikTok-Api/examples/user_example.py", line 18, in user_example
    async for video in user.videos(count=1):
  File "/Users/arhen/Works/test/src/tiktok-public-api-python/TikTok-Api/TikTokApi/api/user.py", line 170, in videos
    resp = await self.parent.make_request(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/arhen/Works/test/src/tiktok-public-api-python/TikTok-Api/TikTokApi/tiktok.py", line 451, in make_request
    raise EmptyResponseException(result, "TikTok returned an empty response")
TikTokApi.exceptions.EmptyResponseException: None -> TikTok returned an empty response

arhen avatar Aug 02 '24 14:08 arhen

This happens to me as well when I run in headless mode, if I set headless to false it works perfectly fine.

NoxRare avatar Aug 02 '24 16:08 NoxRare

This happens to me as well when I run in headless mode, if I set headless to false it works perfectly fine.

It doesn't work for me.

arhen avatar Aug 03 '24 03:08 arhen

I have same issue with this, I just copy the content of https://github.com/davidteather/TikTok-Api/blob/main/examples/user_example.py file and run in my local, and it thrown me the TikTokApi.exceptions.EmptyResponseException: None -> TikTok returned an empty response

my local file:


from TikTokApi import TikTokApi
import asyncio

ms_token = 'my-token'


async def user_example():
    async with TikTokApi() as api:
        await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3)
        user = api.user("sana_sanananoda")
        user_data = await user.info()
        print(user_data)

        async for video in user.videos(count=30):
            print(video)
            print(video.as_dict)

        async for playlist in user.playlists():
            print(playlist)


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

I don't know what happening.

My stacktrace:

Traceback (most recent call last):
  File "/Users/ngoctientran/Desktop/crawler-tiktok/test.py", line 24, in <module>
    asyncio.run(user_example())
  File "/Users/ngoctientran/.pyenv/versions/3.10.3/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/Users/ngoctientran/.pyenv/versions/3.10.3/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/Users/ngoctientran/Desktop/crawler-tiktok/test.py", line 12, in user_example
    user_data = await user.info()
  File "/Users/ngoctientran/Desktop/crawler-tiktok/venv/lib/python3.10/site-packages/TikTokApi/api/user.py", line 76, in info
    resp = await self.parent.make_request(
  File "/Users/ngoctientran/Desktop/crawler-tiktok/venv/lib/python3.10/site-packages/TikTokApi/tiktok.py", line 451, in make_request
    raise EmptyResponseException(result, "TikTok returned an empty response")
TikTokApi.exceptions.EmptyResponseException: None -> TikTok returned an empty response

ngoctienbk avatar Aug 08 '24 08:08 ngoctienbk

This API works in some network environments but does not work in others.

fanjianing avatar Aug 09 '24 09:08 fanjianing

how to fix it it was working couple of months fine

zakrian07 avatar Aug 17 '24 13:08 zakrian07

@davidteather really need help with this bug

zakrian07 avatar Aug 17 '24 15:08 zakrian07

Seems like it is working after I change my IPs. Is it related to proxies, idk.

arhen avatar Aug 19 '24 06:08 arhen

@arhen You mean you changed your machines physical address or just a proxy of any type?

Fetching accounts videos worked super fine for me, until last week where it started throwing the error. I was using TikTokApi 6.2.0 version the whole time

Gereks123 avatar Aug 19 '24 11:08 Gereks123

it still not working for me

zakrian07 avatar Aug 19 '24 12:08 zakrian07

@Gereks123 @zakrian07 did anyone find the answer?

smoltsbohdan avatar Aug 22 '24 14:08 smoltsbohdan

@Gereks123 No, just changes my computer public ips

arhen avatar Aug 26 '24 03:08 arhen

I tried with multiple different IPs, but it doesn't work - even thought it worked fine before.

It there anything we can do to find the cause for this?

mirkoseifert avatar Aug 28 '24 08:08 mirkoseifert

Hey, I have observed some strange behavior in this regard. Normaly i use xfvb-run to use the TikTokAPI headless. But 2 weeks ago it stops with the "TikTok returned an empty response". Today i tried to debug with no real success even not headless. BUT when the window pop up there ( not headless), and i interact with the window ( scroll a littl bit or accept/discline the cookie) it worked again! But only with chromium. (update: some mouse hover is enough) Its not a solution for an automatic script but maybe we can workaround with it ( i don't know how) Can somebody confirm this, too ?

xXZaretXx avatar Aug 28 '24 17:08 xXZaretXx

Okay short update: I add

        time.sleep(10)
        await page.mouse.move(0,0)
        await page.mouse.move(0,100)

below await page.goto(url) in tiktok.py and now it works again. ( maybe need some tuning). even in xvfb

xXZaretXx avatar Aug 28 '24 18:08 xXZaretXx

time.sleep(10) await page.mouse.move(0,0) await page.mouse.move(0,100)

Woaaah, It works!!!!!!!

can you explain your POV why thats problem occurs and why the solution above resolve the problem.

arhen avatar Aug 29 '24 04:08 arhen

image image downgrade playwright using pip install playwright==1.37.0 and run playwright install im getting same issue can any body help me to fix it

zakrian07 avatar Aug 29 '24 09:08 zakrian07

@davidteather thisi s a blocker issue need fix it really need your input as my tool is down from coupe of weeks now

zakrian07 avatar Aug 29 '24 09:08 zakrian07

@arhen

time.sleep(10) await page.mouse.move(0,0) await page.mouse.move(0,100)

Woaaah, It works!!!!!!!

can you explain your POV why thats problem occurs and why the solution above resolve the problem.

Hey, i guess it's some security "feature" from tiktok against bots. My first try was page scrolling but this doesn't work. I only noticed in non-headless mode that my script no longer threw errors after a mouse movement. It was pure luck. And the lines then rebuild this behavour, wait until the page has built up and then “move” the mouse.

xXZaretXx avatar Aug 29 '24 09:08 xXZaretXx

@arhen

time.sleep(10) await page.mouse.move(0,0) await page.mouse.move(0,100)

Woaaah, It works!!!!!!! can you explain your POV why thats problem occurs and why the solution above resolve the problem.

Hey, i guess it's some security "feature" from tiktok against bots. My first try was page scrolling but this doesn't work. I only noticed in non-headless mode that my script no longer threw errors after a mouse movement. It was pure luck. And the lines then rebuild this behavour, wait until the page has built up and then “move” the mouse.

It could be the bot's detection system. I'm not entirely sure how it works but maybe if no action has happened within a small timeframe it's automatically detected as a bot??

It could be the playwright itself, maybe TikTok detects some patterns that resemble the bots behaviour and automatically denies access?

Also, the sleep() and mouse.move() worked like a charm! Ty mate!

Gereks123 avatar Aug 29 '24 11:08 Gereks123

image image downgrade playwright using pip install playwright==1.37.0 and run playwright install im getting same issue can any body help me to fix it

You're not using the sleep() function in the provided picture.

Gereks123 avatar Sep 02 '24 20:09 Gereks123

I think someone need to make a PR for this. @xXZaretXx

arhen avatar Sep 03 '24 09:09 arhen

I think someone need to make a PR for this. @xXZaretXx

@arhen yes, but i hope somebody would improve my little "hacky" workaround. :sweat_smile:

xXZaretXx avatar Sep 03 '24 11:09 xXZaretXx

I think someone need to make a PR for this. @xXZaretXx

@arhen yes, but i hope somebody would improve my little "hacky" workaround. 😅

Bring @davidteather here wdyt about the hackaround.

TLDR; Seems like for specific user.video api, default playwright interaction is not enough.Sometimes it works, and mostly throw the error this issued said above.

With default interactions, I got blocked and can access the video again after several hours or even days. From @xXZaretXx hackaround i can tested that this method works perfectly and I already have automated bot that grab videos of som users every hours since last week.

arhen avatar Sep 04 '24 04:09 arhen

I think someone need to make a PR for this. @xXZaretXx

@arhen yes, but i hope somebody would improve my little "hacky" workaround. 😅

Bring @davidteather here wdyt about the hackaround.

TLDR; Seems like for specific user.video api, default playwright interaction is not enough.Sometimes it works, and mostly throw the error this issued said above.

With default interactions, I got blocked and can access the video again after several hours or even days. From @xXZaretXx hackaround i can tested that this method works perfectly and I already have automated bot that grab videos of som users every hours since last week.

Can confirm on my side, I'm scraping around 100+ accounts daily and this workaround fixed the problem for me.

EDIT: wording

Gereks123 avatar Sep 04 '24 08:09 Gereks123

Everyone on this thread, maybe check out this as well! https://github.com/davidteather/TikTok-Api/issues/1090

I'm running version 6.2.0 for the TikTokAPI with headless=False

Playwright version is 1.43.0.

ALSO, check your msTokens, they expire at a certain time and I had an incident one time where my code was not working due to the expired msToken. The API itself sometimes has issues with fetching the token from the browser itself.

Create an array of msTokens and just feed it to the create_sessions method

Example:
`ms_tokens = [
    "token_1", "token_2"
]

await api.create_sessions(ms_tokens=ms_tokens, num_sessions=1, sleep_after=3, headless=False)

Gereks123 avatar Sep 11 '24 09:09 Gereks123

Hi I already add this solution but still cannot get a data from tiktok. :(

Okay short update: I add

        time.sleep(10)
        await page.mouse.move(0,0)
        await page.mouse.move(0,100)

below await page.goto(url) in tiktok.py and now it works again. ( maybe need some tuning). even in xvfb

My code image

Error image

Warhead007 avatar Sep 16 '24 08:09 Warhead007

@Warhead007 Which Browser do you use? I'm using chromium (create_sessions)

maybe you need to update your ms_token

xXZaretXx avatar Sep 16 '24 08:09 xXZaretXx

Hi @xXZaretXx ,

I using a chromium. And can you explain how to get and update a ms_token? I'm a new to this project. Thank you.

image

Warhead007 avatar Sep 16 '24 09:09 Warhead007

Hi @xXZaretXx ,

I using a chromium. And can you explain how to get and update a ms_token? I'm a new to this project. Thank you.

image

You can get the ms_token by going to your actual browser and then navigate to tiktoks actual page.

Then open your developer tools and look for a cookie named ms_Token or something like that.

Also, looking at the picture, when you create your tiktok session, use the parameter "headless=False"

Gereks123 avatar Sep 16 '24 09:09 Gereks123

Hi, @Gereks123, @arhen , @xXZaretXx.

Have you been able to download the videos? My code can get the information about the videos, but when I try to download them, it says access denied. I modified the tiktok.py file with the modification suggested in this discussion, but I still can't download the videos.

If you can, how do you download them?

rockfly830 avatar Sep 27 '24 01:09 rockfly830