TikTok-Api
TikTok-Api copied to clipboard
[FEATURE_REQUEST] - Use browser cookies to overcome "captcha" issue
Is your feature request related to a problem? Please describe. I am getting a lot of "tiktok blocks this request by displaying captcha" errors but it works fine on browser. So, it would be really amazing to have a custom cookie, device id, session id etc that can be exported and then used by Tiktok-API to then run the code since having a tiktok account and using those cookies in a browser works flawleslly.
Thanks.
same problem
remove the verifyFp parameter from url
remove the verifyFp parameter from url
I already did. Also looks like the cookies are changed. So this version does not work anyway.
I am getting a lot of captcha errors too, I found the cause, the SIGI_STATE was modified at least on some version of the page or maybe TikTok is doing an a/b test, maybe next week I will upload a patch because I have to fix this.
@ctaity Hey, any update on that? Having the same problem and not quite sure what to do about it. It recommends using a proxy or a custom_verify_fp argument, but I'm already doing both of those and still no luck. I can hardly get a request through to TikTok because of all these captchas.
@benostarzec I am usuing API method to get user info and I use cookies if you are interested I can post my modifications.
@benostarzec I am usuing API method to get user info and I use cookies if you are interested I can post my modifications.
yes, please that would be grand!
Hey @ctaity. Could you please share your mods? Thanks
@ctaity any update, were you able to get through the error?
hi everyone , these are my modifications: @rakemen @smit-celvix
import random
from http import cookiejar
from urllib.parse import quote, urlencode
from TikTokApi import TikTokApi
from TikTokApi.api.user import User
from TikTokApi.browser_utilities.browser import browser
async def patch_create_context(self, set_useragent=False):
iphone = self.playwright.devices["iPhone 11 Pro"]
iphone["viewport"] = {
"width": random.randint(320, 1920),
"height": random.randint(320, 1920),
}
iphone["device_scale_factor"] = random.randint(1, 3)
iphone["is_mobile"] = random.randint(1, 2) == 1
iphone["has_touch"] = random.randint(1, 2) == 1
iphone["bypass_csp"] = True
iphone["ignore_https_errors"] = True
context = await self.browser.new_context(**iphone)
if set_useragent:
self.user_agent = iphone["user_agent"]
return context
def patch_user_info_full(self, **kwargs) -> dict:
"""
Returns a dictionary of information associated with this User.
Includes statistics about this user.
Example Usage
```py
user_data = api.user(username='therock').info_full()
```
"""
# TODO: Find the one using only user_id & sec_uid
if not self.username:
raise TypeError(
"You must provide the username when creating this class to use this method.")
quoted_username = quote(self.username)
query = {"uniqueId": quoted_username, "secUid": "",
"msToken": User.parent._get_cookies(**kwargs)["msToken"]}
path = f"api/user/detail/?{User.parent._add_url_params()}&{urlencode(query)}"
res = User.parent.get_data(path, subdomain="m", **kwargs)
return res["userInfo"]
class AltoTikTokApi(TikTokApi):
cookie_file: str
def __init__(self, *args, cookie_file: str = None, **kwargs):
super().__init__(*args, **kwargs)
self.cookie_file = cookie_file
self._load_cookies_from_file()
def _load_cookies_from_file(self):
if self.cookie_file is None:
self.cookies = {}
else:
cookie_jar = cookiejar.MozillaCookieJar(self.cookie_file)
cookie_jar.load()
self.cookies = {cookie.name: cookie.value for cookie in cookie_jar}
def _get_cookies(self, **kwargs):
# final_cookies = dict(self.cookies)
# final_cookies.update(kwargs)
# return final_cookies
return self.cookies
def get_cookie_file_name(self) -> str:
return self.cookie_file
browser._create_context = patch_create_context
AltoTikTokApi.user.info_full = patch_user_info_full
I use the extension cookies.txt to download the cookies from TikTok, first I logged in the user, then I download the cookies.
I think we need both methods to get the user info full, with API and web scraping, if @davidteather agrees, maybe I could do a pull request next week
hi everyone , these are my modifications: @rakemen @smit-celvix
import random from http import cookiejar from urllib.parse import quote, urlencode from TikTokApi import TikTokApi from TikTokApi.api.user import User from TikTokApi.browser_utilities.browser import browser async def patch_create_context(self, set_useragent=False): iphone = self.playwright.devices["iPhone 11 Pro"] iphone["viewport"] = { "width": random.randint(320, 1920), "height": random.randint(320, 1920), } iphone["device_scale_factor"] = random.randint(1, 3) iphone["is_mobile"] = random.randint(1, 2) == 1 iphone["has_touch"] = random.randint(1, 2) == 1 iphone["bypass_csp"] = True iphone["ignore_https_errors"] = True context = await self.browser.new_context(**iphone) if set_useragent: self.user_agent = iphone["user_agent"] return context def patch_user_info_full(self, **kwargs) -> dict: """ Returns a dictionary of information associated with this User. Includes statistics about this user. Example Usage ```py user_data = api.user(username='therock').info_full() ``` """ # TODO: Find the one using only user_id & sec_uid if not self.username: raise TypeError( "You must provide the username when creating this class to use this method.") quoted_username = quote(self.username) query = {"uniqueId": quoted_username, "secUid": "", "msToken": User.parent._get_cookies(**kwargs)["msToken"]} path = f"api/user/detail/?{User.parent._add_url_params()}&{urlencode(query)}" res = User.parent.get_data(path, subdomain="m", **kwargs) return res["userInfo"] class AltoTikTokApi(TikTokApi): cookie_file: str def __init__(self, *args, cookie_file: str = None, **kwargs): super().__init__(*args, **kwargs) self.cookie_file = cookie_file self._load_cookies_from_file() def _load_cookies_from_file(self): if self.cookie_file is None: self.cookies = {} else: cookie_jar = cookiejar.MozillaCookieJar(self.cookie_file) cookie_jar.load() self.cookies = {cookie.name: cookie.value for cookie in cookie_jar} def _get_cookies(self, **kwargs): # final_cookies = dict(self.cookies) # final_cookies.update(kwargs) # return final_cookies return self.cookies def get_cookie_file_name(self) -> str: return self.cookie_file browser._create_context = patch_create_context AltoTikTokApi.user.info_full = patch_user_info_full
I use the extension cookies.txt to download the cookies from TikTok, first I logged in the user, then I download the cookies.
@ctaity I'm experimenting the same problem using your solution, could you tell us how can you fix it please? Thx.
how is the patch_create_context and patch_user_info_full given the parameteter self and where is playwright imported and where is playwright.devies in the playwright library documentation? Thank you
Is there any updates? I am facing the same issue
to bypass captcha see my telegram bot for tiktok capthca solve https://t.me/cap_watch_dog_bot
V6 uses browser cookies