Open nedoai opened 5 months ago
share the code please
share the code please
from TikTokApi import TikTokApi
import asyncio
import os
import logging
import pickle
ms_token = os.environ.get("ms_token", None)
def load_cookies(cookie_path):
cookies = []
if cookie_path:
with open(cookie_path, "rb") as f:
cookie_data = pickle.load(f)
cookies.append(cookie_data)
return cookies
cookies = load_cookies(r"cookies\aicovermodels_cookies")
async def trending_videos():
async with TikTokApi() as api:
await api.create_sessions(ms_tokens=[ms_token], cookies=cookies, num_sessions=1, sleep_after=3, executable_path=r"chrome-win64\chrome.exe")
user = api.user(username="therock")
user_info = await user.info()
print(user_info)
if __name__ == "__main__":
asyncio.run(trending_videos())
I also removed the “correct” cookie formatting from the code in TikTokApi:
If cookies is not None:
await context.add_cookies(cookies)
I removed it because it doesn't format cookies, but makes my normal cookies look like something else.
share the code please
from TikTokApi import TikTokApi import asyncio import os import logging import pickle ms_token = os.environ.get("ms_token", None) def load_cookies(cookie_path): cookies = [] if cookie_path: with open(cookie_path, "rb") as f: cookie_data = pickle.load(f) cookies.append(cookie_data) return cookies cookies = load_cookies(r"cookies\aicovermodels_cookies") async def trending_videos(): async with TikTokApi() as api: await api.create_sessions(ms_tokens=[ms_token], cookies=cookies, num_sessions=1, sleep_after=3, executable_path=r"chrome-win64\chrome.exe") user = api.user(username="therock") user_info = await user.info() print(user_info) if __name__ == "__main__": asyncio.run(trending_videos())
I also removed the “correct” cookie formatting from the code in TikTokApi:
If cookies is not None: await context.add_cookies(cookies)
I removed it because it doesn't format cookies, but makes my normal cookies look like something else.
Solved the problem. The problem occurs due to msToken embedding when using cookies (why? I don't know).
What I did: Just set msToken to None in default requests. Surprisingly everything works. I knew that msToken could be any value, but this....
Now my sign url's looks like this:
https://www.tiktok.com/api/user/detail/?aid=1988&app_language=ru-RU&app_name=tiktok_web&browser_language=ru-RU&browser_name=Mozilla&browser_online=true&browser_platform=Win32&browser_version=Mozilla%2F5.0%20%28Windows%20NT%2010.0%3B%20Win64%3B%20x64%29%20AppleWebKit%2F537.36%20%28KHTML%2C%20like%20Gecko%29%20Chrome%2F115.0.0.0%20Safari%2F537.36&channel=tiktok_web&cookie_enabled=true&device_id=9142832745118890963&device_platform=web_pc&focus_state=true&from_page=user&history_len=9&is_fullscreen=false&is_page_visible=true&language=ru-RU&os=Win32&priority_region=&referer=®ion=US&screen_height=673&screen_width=1310&tz_name=Europe%2FKiev&webcast_language=ru-RU&secUid=&uniqueId=therock&msToken=None&X-Bogus=WB2uWMgrGlu5X5It
When trying to create a session with the cookies argument in subsequent requests (e.g. user(username“...”).info()) - will constantly return an error TikTok returned an empty response. At the same time there is no problem with cookies. Cookies are loaded with the session successfully, but fetch request returns None.
If anyone knows how to fix it please post here.
What I mean and what it looks like:
Response without cookie params in api.create_sessions():
{'extra': {'fatal_item_ids': [], 'logid': '20240609192410B0FDA25FA48E243D8157', 'now': 1717961051000}, 'log_pb': {'impr_id': '20240609192410B0FDA25FA48E243D8157'}, 'shareMeta': {'desc': '@therock Подписчики: 74.4m, подписки: 6, лайки: 547.5m — The Rock создает классные короткие видеоролики', 'title': 'The Rock в TikTok'}, 'statusCode': 0, 'status_code': 0, 'status_msg': '', 'userInfo': {'stats': {'diggCount': 0, 'followerCount': 74400000, 'followingCount': 6, 'friendCount': 6, 'heart': 547500000, 'heartCount': 547500000, 'videoCount': 261}, 'user': {'avatarLarger': 'https://p16-sign-va.tiktokcdn.com/tos-maliva-avt-0068/7310325298631802885~c5_1080x1080.jpeg?lk3s=a5d48078&nonce=12314&refresh_token=12770e072623138a8f99fa6f74590705&x-expires=1718132400&x-signature=i%2BijYRU38bmpxmZoi4%2FW3ycFcPY%3D&shp=a5d48078&shcp=81f88b70', 'avatarMedium': 'https://p16-sign-va.tiktokcdn.com/tos-maliva-avt-0068/7310325298631802885~c5_720x720.jpeg?lk3s=a5d48078&nonce=96002&refresh_token=77be63ff105e59af693ffcdc024c308b&x-expires=1718132400&x-signature=oE1mn3Xw0PYdeYnH3FchtklQeRE%3D&shp=a5d48078&shcp=81f88b70', 'avatarThumb': 'https://p16-sign-va.tiktokcdn.com/tos-maliva-avt-0068/7310325298631802885~c5_100x100.jpeg?lk3s=a5d48078&nonce=83312&refresh_token=74282fe6d3d5217e831d8b73d0b6dd6a&x-expires=1718132400&x-signature=4kJTh9SJNLngFwDyFsVuZ%2F%2B%2BwjI%3D&shp=a5d48078&shcp=81f88b70', 'bioLink': {'link': 'therock.komi.io', 'risk': 3}, 'canExpPlaylist': True, 'commentSetting': 0, 'commerceUserInfo': {'commerceUser': False}, 'downloadSetting': 0, 'duetSetting': 0, 'followingVisibility': 1, 'ftc': False, 'id': '6745191554350760966', 'isADVirtual': False, 'isEmbedBanned': False, 'nickname': 'The Rock', 'openFavorite': False, 'privateAccount': False, 'profileEmbedPermission': 1, 'profileTab': {'showPlayListTab': True}, 'relation': 0, 'secUid': 'MS4wLjABAAAAM3R2BtjzVT-uAtstkl2iugMzC6AtnpkojJbjiOdDDrdsTiTR75-8lyWJCY5VvDrZ', 'secret': False, 'signature': 'CEO of #RockTok\nLife’s so much sweeter when you’re not an asshole', 'stitchSetting': 0, 'ttSeller': False, 'uniqueId': 'therock', 'verified': True}}}
Response with cookie params in api.create_sessions():