-
-
Notifications
You must be signed in to change notification settings - Fork 977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] - Loading session via cookies #1160
Comments
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:
|
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():
The text was updated successfully, but these errors were encountered: