Russell-Newton / TikTokPy

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

[QUESTION] Access Denied Error When Downloading Video #44

Open muchtoyearn opened 1 year ago

muchtoyearn commented 1 year ago

Hello. I'm certain this is a simple issue of just passing through my s_v_web_id and tt_chain_token, but I'm having trouble doing so.

Basically the code runs without error, but I get an Access Denied error when trying to download a TikTok Video. What's the work around here?


import io
import glob
import os
import urllib.request
from os import path

import aiohttp
from tiktokapipy.async_api import AsyncTikTokAPI
from tiktokapipy.models.video import Video

link = "https://www.tiktok.com/@prestoneckhardt/video/7203852306889133358"
directory = r"C:\Users\peckh\Desktop\ECKHARDT CAPITAL LLC"

async def save_video(video: Video):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0;Win64) AppleWebkit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'}
    async with aiohttp.ClientSession() as session:
        async with session.get(video.video.download_addr, headers=headers) as resp:
            data = await resp.read()
            print(data)  # print the response data
            with open(os.path.join(directory, f'{video.id}.mp4'), 'wb') as f:
                f.write(data)
            print(f"Video {video.id} has been downloaded.")

async def download_video():
    async with AsyncTikTokAPI() as api:
        video: Video = await api.video(link)
        if video.image_post:
            downloaded = await save_slideshow(video)
        else:
            downloaded = await save_video(video)

asyncio.run(download_video())```
Russell-Newton commented 1 year ago

TikTok doesn't seem to use the s_v_web_id token anymore. The tt_chain_token cookie and a referrer header is all you need. The documentation includes a note for a workaround, provided in #35: https://tiktokpy.readthedocs.io/en/stable/users/usage.html#download-videos-and-slideshows.

If this doesn't work for you, please let me know.

Russell-Newton commented 8 months ago

This may be fixed in version 0.2.4 with the video download function. It doesn't work on slideshows and requires the optional yt-dlp dependency. This can be installed with pip install yt-dlp or pip install tiktokapipy[download]. It may not work 100% depending on where you are, but it works well for me.