Russell-Newton / TikTokPy

Extract data from TikTok without needing any login information or API keys.
https://pypi.org/project/tiktokapipy/
MIT License
213 stars 28 forks source link

Some video links are returning 'Forbidden' #24

Closed Enes-Kayiklik closed 1 year ago

Enes-Kayiklik commented 1 year ago

Some video links are returning '403 Forbidden' when I try to open them. After sending one more request to get video information, it fixes but it wasting time.

Here is my code:

def get_tiktok_media(tiktok_username: str, max_count: int):
    try:
        result = []
        with TikTokAPI() as api:
            user = api.user(tiktok_username, max_count)
            for video in user.videos:
                result_video = video
                while result_video.video.play_addr.endswith("chain_token") and result_video.video.download_addr.endswith("chain_token"):
                    result_video = api.video(f"https://www.tiktok.com/@{video.author}/video/{result_video.id}")

                result.append(result_video)
        return result
    except Exception as e:
        return e

I realize patterns between working videos and not working videos.

Sample working video link: https://v16-webapp.tiktok.com/612dc7b3bd400a2ccd84af58fdbba950/63d15716/video/tos/useast2a/tos-useast2a-pve-0037c001-aiso/o8DYgoPAliHy5aGSOe2Erh0KfZrfkOEQAHGZeC/?a=1988&ch=0&cr=0&dr=0&lr=tiktok&cd=0%7C0%7C1%7C0&cv=1&br=4260&bt=2130&cs=0&ds=3&ft=4b~OyMlh8Zmo0Cd6I64jVhLWypWrKsdm&mime_type=video_mp4&qs=0&rc=MzY7NmY4OGZnZjY8ZzxmZkBpam87NWU6ZnA6aTMzZjgzM0A0YGIyM182XjExLWExNDUvYSNhLmwzcjRnMTVgLS1kL2Nzcw%3D%3D&l=20230125102117B681AB00030843ACF23F&btag=80000

Sample not working video link: https://v16-webapp-prime.tiktok.com/video/tos/useast2a/tos-useast2a-pve-0037-aiso/oEbqy7f2KjVLQQDyUec8GCmnBHD80oVhnWPAgD/?a=1988&ch=0&cr=0&dr=0&lr=tiktok&cd=0%7C0%7C1%7C0&cv=1&br=2520&bt=1260&cs=0&ds=3&ft=4fUEKMlh8Zmo03d6I64jVVO1ZpWrKsdm&mime_type=video_mp4&qs=0&rc=NmY4NjNlZDM3M2VnOGU3aUBpM3d2czg6ZnRkaTMzZjgzM0BfMl9gLl9hXzQxMzUxXzQzYSNzcF9kcjRnZzVgLS1kL2Nzcw%3D%3D&btag=80000&expire=1674663675&l=202301251021026E4C2DACC26490AAE1A6&ply_type=2&policy=2&signature=e303281836dfc4b2b8d8acfe4beb9963&tk=tt_chain_token

not working videos are ends with chain_token

I'll be happy if anybody can help me about this.

tiktokapipy version is 0.1.10.post1

Russell-Newton commented 1 year ago

This isn't something I've encountered in my testing. It's possible you may need to use a proxy, which should be doable in version 0.1.11. It's worth noting that these links do go stale after a time. I've seen them go stale after anywhere from 30 minutes to 2 hours.

jc-a3s commented 1 year ago

It seems to be permanently blocked probably by some anti-bot mechanisms. 1-2 weeks ago I was able to download some videos (not all of them due to Forbidden issue) but now all videos I'm trying to grab, are blocked. Even if I re-generate link (multiple trials) like @Enes-Kayiklik

Russell-Newton commented 1 year ago

Again, I'm not having this problem, so I don't have a good way to debug it. I need some more information:

  1. How soon are you trying to download the video after getting the address with TikTokPy? Are you saving the address somewhere to be used later?
  2. What country/region are you in?
  3. Are you able to access TikTok and watch videos normally through a desktop browser?
  4. How are you trying to download the videos? What library/libraries are you using? Providing a code snippet that includes both the use of TikTokPy and the video downloading would be most helpful here.

If someone experiencing this issue can answer all of these questions I'll be better able to address the issue.

ssyyhhrr commented 1 year ago

Also experiencing this issue, I tried re-generating the link multiple times with no luck.

  1. Immediately, I've tried downloading the videos using aiohttp demonstrated in the documentation and also manually accessing the URL with no luck.
  2. UK
  3. Yes
  4. 
    import asyncio
    import aiofiles
    import aiohttp
    from tiktokapipy.async_api import AsyncTikTokAPI
    from tiktokapipy.models.video import Video

hashtag = "technews"

async def save_video(video: Video): async with aiohttp.ClientSession() as session: async with session.get(video.video.download_addr) as resp: content = await resp.content.read() async with aiofiles.open("video.mp4", mode="wb") as f: await f.write(content)

async def main(): async with AsyncTikTokAPI() as api: challenge = await api.challenge(hashtag) async for video in challenge.videos: creator = await video.creator() print("===================================") print("Username:", creator.unique_id) print("Caption:", video.desc) await save_video(video) break

if name == 'main': asyncio.run(main())

jc-a3s commented 1 year ago
  1. Immediately after URL retrieval. I also checked expiration parameter in URL and it's around 6h but I tried to download video at most after seconds from URL generation.
  2. Poland
  3. It depends. I can access videos (from web browser) via the following URL: https://www.tiktok.com/tag/funnyvideos but when I try to search (https://www.tiktok.com/search/user?q=%23funnyvideos&t=1676928433374) and I click on Videos or The best tab, TikTok requires signing in.
  4. It's one of simple examples but I tried also in different ways. All with the same results: 403 - Forbidden.
    
    from tiktokapipy.api import TikTokAPI
    import urllib.request as req

with TikTokAPI() as api: videos_wrapper = api.challenge("funnyvideos", video_limit=1) for video_el in videos_wrapper.videos: link = f"https://www.tiktok.com/@{video_el.author}/video/{video_el.id}" video = api.video(link) req.urlretrieve(video.video.download_addr, "output.mp4")

Russell-Newton commented 1 year ago

@papayyg found a solution that works for them in #35. Hopefully this can work for you. I'll add it to the documentation soon.

Enes-Kayiklik commented 1 year ago

Yes #35 worked for me. Thank you for your response.