TikTok-Api icon indicating copy to clipboard operation
TikTok-Api copied to clipboard

[FEATURE_REQUEST] - Use browser cookies to overcome "captcha" issue

Open smit-celvix opened this issue 2 years ago • 14 comments

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.

smit-celvix avatar Jun 25 '22 17:06 smit-celvix

same problem

micr0ded avatar Jun 26 '22 08:06 micr0ded

remove the verifyFp parameter from url

Altimis avatar Jun 27 '22 17:06 Altimis

remove the verifyFp parameter from url

I already did. Also looks like the cookies are changed. So this version does not work anyway.

smit-celvix avatar Jun 29 '22 01:06 smit-celvix

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 avatar Jul 01 '22 18:07 ctaity

@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 avatar Jul 07 '22 19:07 benostarzec

@benostarzec I am usuing API method to get user info and I use cookies if you are interested I can post my modifications.

ctaity avatar Jul 08 '22 01:07 ctaity

@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!

smit-celvix avatar Jul 08 '22 04:07 smit-celvix

Hey @ctaity. Could you please share your mods? Thanks

smit-celvix avatar Jul 14 '22 06:07 smit-celvix

@ctaity any update, were you able to get through the error?

rakemen avatar Jul 25 '22 20:07 rakemen

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 avatar Jul 25 '22 23:07 ctaity

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

ctaity avatar Jul 25 '22 23:07 ctaity

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.

AranchaBuendia avatar Jul 28 '22 10:07 AranchaBuendia

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

kc-campbell avatar Aug 15 '22 21:08 kc-campbell

Is there any updates? I am facing the same issue

ezgiturali avatar Sep 16 '22 08:09 ezgiturali

to bypass captcha see my telegram bot for tiktok capthca solve https://t.me/cap_watch_dog_bot

PolcovnicGauss avatar Oct 17 '22 20:10 PolcovnicGauss

V6 uses browser cookies

davidteather avatar Aug 08 '23 22:08 davidteather