davidteather / TikTok-Api

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

Access Denied for Download/get_Video #315

Closed Goldrest closed 3 years ago

Goldrest commented 3 years ago

Hi !

I have some issues to download videos from the API with others than api.trending()'s Tiktok Object. After some research i found out that the Tiktok Object from api.trending and others (like api.ByHashtag()) section are different.

First

from Trending contains 23 keys from others contains 24 keys

it seems that "Challenges" doesn't exist anymore.

Second

the format of the downloadAddr is different and the one from others had access denied when i call api.get_Video_By_DownloadURL or get_Video_By_Tiktok. when i print the data (who suppose to be the number of byte of the video) i got : _b'<HTML><HEAD>\n<TITLE>Access Denied</TITLE>\n</HEAD><BODY>\n<H1>Access Denied</H1>\n \nYou don\'t have permission to access_

After some research (on others API/Tiktok scraper) it seems that i need to set correct header but i dont know how to do that.

Maybe i'm doing things wrong ?

Thank you for your help !

issue-label-bot[bot] commented 3 years ago

Issue-Label Bot is automatically applying the label bug to this issue, with a confidence of 0.53. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

davidteather commented 3 years ago

Provide code please

Goldrest commented 3 years ago

the code

from TikTokApi import TikTokApi
from TikTokApi.browser import set_async
set_async()
trending = api.trending(count=2)
foryou = api.byHashtag("foryou", count=2)
print("TRENDING first Tiktok object keys :")
print(trending[0].keys())
print("TRENDING downloadAddr :\n" + trending[0]['video']['downloadAddr'])
print()
print("FORYOU first Tiktok objects keys :")
print(foryou[0].keys())
print("FORYOU downloadAddr: \n" + foryou[0]['video']['downloadAddr'])
print()
print("TREND get_Video_By_DownloadURL")
data_trend = api.get_Video_By_DownloadURL(
    trending[0]["video"]["downloadAddr"]
)
#print(data_trend) # NO NEED because IOPub data rate exceeded so too big to print because video data
print()
print("FORYOU get_Video_By_DownloadURL")
data_foryou = api.get_Video_By_DownloadURL(
    foryou[0]["video"]["downloadAddr"]
)
print()
print("FORYOU data")
print(data_foryou)
print(data)

the ouput

TRENDING first Tiktok object keys :
dict_keys(['id', 'desc', 'createTime', 'video', 'author', 'music', 'stats', 'originalItem', 'officalItem', 'textExtra', 'secret', 'forFriend', 'digged', 'itemCommentStatus', 'showNotPass', 'vl1', 'itemMute', 'authorStats', 'privateItem', 'duetEnabled', 'stitchEnabled', 'shareEnabled', 'isAd'])
TRENDING downloadAddr :
https://v77.tiktokcdn.com/13c8be15a49c35d004055e31b6e1b4ce/5fa47023/video/tos/useast2a/tos-useast2a-ve-0068c002/167f85c803c0471ca7015e7a8924ecf9/?a=1233&br=2706&bt=1353&cr=0&cs=0&cv=1&dr=0&ds=3&er=&l=202011051535160101902182225C02CC8C&lr=tiktok_m&mime_type=video_mp4&qs=0&rc=anE5bm1neHd2djMzNDczM0ApM2Y6Nmc4Z2U8N2U6NGRnPGdpLWdqLi9xX2xfLS1gMTZzczQtXjUuMy1hXjU1M18uLjA6Yw%3D%3D&vl=&vr=

FORYOU first Tiktok objects keys :
dict_keys(['id', 'desc', 'createTime', 'video', 'author', 'music', 'challenges', 'stats', 'originalItem', 'officalItem', 'textExtra', 'secret', 'forFriend', 'digged', 'itemCommentStatus', 'showNotPass', 'vl1', 'itemMute', 'authorStats', 'privateItem', 'duetEnabled', 'stitchEnabled', 'shareEnabled', 'isAd'])
FORYOU downloadAddr: 
https://v16-web.tiktok.com/video/tos/useast2a/tos-useast2a-ve-0068/c7eaeb4425b04194b1d4cf6d5e4fcff8/?a=1988&br=5222&bt=2611&cr=0&cs=0&cv=1&dr=0&ds=3&er=&expire=1604612144&l=202011051535260101902080474102B993&lr=tiktok_m&mime_type=video_mp4&policy=2&qs=0&rc=M3NlNm47NDs2cjMzZTczM0ApOjg0NDQ6aGU3NzdpOGhnPGcxLS5uZC1wZzRfLS0wMTZzc2E1Yi9iXjAzYC1fXmAxYV46Yw%3D%3D&signature=85d099229521460a5dcf71a2bdd43dc3&tk=tt_webid_v2&vl=&vr=

TREND get_Video_By_DownloadURL
961841351

FORYOU get_Video_By_DownloadURL
736741080

FORYOU data
b'<HTML><HEAD>\n<TITLE>Access Denied</TITLE>\n</HEAD><BODY>\n<H1>Access Denied</H1>\n \nYou don\'t have permission to access "http&#58;&#47;&#47;v16&#45;web&#46;tiktok&#46;com&#47;video&#47;tos&#47;useast2a&#47;tos&#45;useast2a&#45;ve&#45;0068&#47;c7eaeb4425b04194b1d4cf6d5e4fcff8&#47;&#63;" on this server.<P>\nReference&#32;&#35;18&#46;4cf41202&#46;1604590866&#46;d79ffa\n</BODY>\n</HTML>\n'

Thanks you for you reactivity !

davidteather commented 3 years ago

https://github.com/davidteather/TikTok-Api/issues/311#issuecomment-721164493

Goldrest commented 3 years ago

Thanks you problem resolved !

However it's the first time i see that custom_did mentioned. There is no trace in the documention and/or examples What is its purpose ?

davidteather commented 3 years ago

I'm updating the examples in a few minutes. The custom_did parameter is a cookie that TikTok checks for when downloading a video and it must match the cookie that retrieved the data from. So you have to keep track of the cookie you're using for each request to the API if you want to download the videos.

Goldrest commented 3 years ago

Okay ! Thanks for this information !