davidteather / TikTok-Api

The Unofficial TikTok API Wrapper In Python
https://davidteather.github.io/TikTok-Api
MIT License
4.91k stars 984 forks source link

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

Open arhen opened 3 months ago

arhen commented 3 months ago

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/python@3.11/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/python@3.11/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/python@3.11/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
NoxRare commented 3 months ago

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

arhen commented 3 months ago

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.

ngoctienbk commented 3 months ago

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
fanjianing commented 3 months ago

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

zakrian07 commented 3 months ago

how to fix it it was working couple of months fine

zakrian07 commented 3 months ago

@davidteather really need help with this bug

arhen commented 3 months ago

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

Gereks123 commented 3 months ago

@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

zakrian07 commented 3 months ago

it still not working for me

smoltsbohdan commented 3 months ago

@Gereks123 @zakrian07 did anyone find the answer?

arhen commented 3 months ago

@Gereks123 No, just changes my computer public ips

mirkoseifert commented 3 months ago

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?

xXZaretXx commented 3 months ago

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 commented 3 months ago

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

arhen commented 3 months ago

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.

zakrian07 commented 3 months ago

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 commented 3 months ago

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

xXZaretXx commented 3 months ago

@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.

Gereks123 commented 3 months ago

@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 commented 2 months ago

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.

arhen commented 2 months ago

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

xXZaretXx commented 2 months ago

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:

arhen commented 2 months ago

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.

Gereks123 commented 2 months ago

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 commented 2 months ago

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)
Warhead007 commented 2 months ago

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

xXZaretXx commented 2 months ago

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

maybe you need to update your ms_token

Warhead007 commented 2 months ago

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

Gereks123 commented 2 months ago

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"

rockfly830 commented 2 months ago

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?

arhen commented 1 month ago

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

Check you ms_token, try to change to a new one.

VMASPAD commented 1 month ago

Hello! I tried your method at the date of this comment and it still does not work, we will have to find a way to prevent it from detecting it as a bot, as it seems to find the user (in my case) but when entering it comes out, it must be that it is trying to access some links so quickly or robustly that it detects it as a bot.

Gereks123 commented 1 month ago

I'm running: Python: 3.12, TikTokApi: 6.5.2 with the given changes in this discussion Playwright: 1.39.0 OS: Windows 11

Everything works perfectly for me :/

VMASPAD commented 1 month ago

Yeah! It seems to be working since I just tested it and it works but I don't know if it works with the changes made by the partner.

EDIT > Stopped working xd only tried it once with the example code

Gereks123 commented 1 month ago

Yeah! It seems to be working since I just tested it and it works but I don't know if it works with the changes made by the partner.

EDIT > Stopped working xd only tried it once with the example code

Always make sure there aren't any dependencies interfering with you code. Always use a virtual enviornment for a fresh start.

When installing the TikTokApi, install pip install playwright==1.39.0 and then run python -m playwright install depending on your OS, you might have to use "python3". On windows "python -m playwright install" should be enough