Skip to content
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

TikTok blocks this request displaying a Captcha even with custom_verifyFp and proxy #787

Closed
kostyakoz opened this issue Jan 3, 2022 · 20 comments
Labels
bug Something isn't working

Comments

@kostyakoz
Copy link

kostyakoz commented Jan 3, 2022

Everything was fine until last few days.

from TikTokApi import TikTokApi
import json
import logging
tiktok_api = TikTokApi.get_instance(request_delay=1)

tiktoks = tiktok_api.by_username('japanontiktok', count=1, custom_verifyFp="...", proxy='http://...', use_test_endpoints=True)

for tiktok in tiktoks[:1]:
    print(json.dumps(tiktok))

Error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/TikTokApi/tiktok.py", line 1333, in get_user
    j_raw = self.__extract_tag_contents(r.text)
  File "/usr/local/lib/python3.8/dist-packages/TikTokApi/tiktok.py", line 1553, in __extract_tag_contents
    nonce = html.split(nonce_start)[1].split(nonce_end)[0]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "tests.py", line 220, in <module>
    tiktoks = tiktok_api.by_username('japanontiktok', count=1, custom_verifyFp="...", proxy='http://...', use_test_endpoints=True)
  File "/usr/local/lib/python3.8/dist-packages/TikTokApi/tiktok.py", line 700, in by_username
    data = self.get_user_object(username, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/TikTokApi/tiktok.py", line 1301, in get_user_object
    return self.get_user(username, **kwargs)["userInfo"]["user"]
  File "/usr/local/lib/python3.8/dist-packages/TikTokApi/tiktok.py", line 1339, in get_user
    raise TikTokCaptchaError()
TikTokApi.exceptions.TikTokCaptchaError: TikTok blocks this request displaying a Captcha 
Tip: Consider using a proxy or a custom_verifyFp as method parameters

The most interesting thing there is that I can partly see obtained data from TikTok because its print HTML page in console before the error. You can see full response here: https://gist.githubusercontent.com/kostyakoz/1d5a7339217d646f1556d9fd63bcf7a8/raw/b18f4ae5e869325bb746c48fae102c8786b43fdb/error.py

My question is why obtained data not returned and I have only error?

@kostyakoz kostyakoz added the bug Something isn't working label Jan 3, 2022
@botcoder-sys
Copy link

I am facing the same problem as well. It was working fine with custom_verifyFp earlier. But, I think a week ago, it starts breaking. Today, I run the by.username() in loop until it succeeded with custom_verifyFp and it worked once after each 20-30 tries. Sometimes changing proxy also helps but I think there is some issue with API. I have the exact same error as @kostyakoz Showed above.

@angelopouloschristos
Copy link

there is the same issue but in the "closed" section because someone closed it without a solution
We are still waiting for a solution a lot of poeple have the same issue and usualy it returns a HTML response that correspond to the use profil page and it does not download anything. The captcha is not the problem here. They made something with their API

@joseantgv
Copy link

joseantgv commented Jan 3, 2022

Everything was fine until last few days.

from TikTokApi import TikTokApi
import json
import logging
tiktok_api = TikTokApi.get_instance(request_delay=1)

tiktoks = tiktok_api.by_username('japanontiktok', count=1, custom_verifyFp="...", proxy='http://...', use_test_endpoints=True)

for tiktok in tiktoks[:1]:
    print(json.dumps(tiktok))

Error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/TikTokApi/tiktok.py", line 1333, in get_user
    j_raw = self.__extract_tag_contents(r.text)
  File "/usr/local/lib/python3.8/dist-packages/TikTokApi/tiktok.py", line 1553, in __extract_tag_contents
    nonce = html.split(nonce_start)[1].split(nonce_end)[0]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "tests.py", line 220, in <module>
    tiktoks = tiktok_api.by_username('japanontiktok', count=1, custom_verifyFp="...", proxy='http://...', use_test_endpoints=True)
  File "/usr/local/lib/python3.8/dist-packages/TikTokApi/tiktok.py", line 700, in by_username
    data = self.get_user_object(username, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/TikTokApi/tiktok.py", line 1301, in get_user_object
    return self.get_user(username, **kwargs)["userInfo"]["user"]
  File "/usr/local/lib/python3.8/dist-packages/TikTokApi/tiktok.py", line 1339, in get_user
    raise TikTokCaptchaError()
TikTokApi.exceptions.TikTokCaptchaError: TikTok blocks this request displaying a Captcha 
Tip: Consider using a proxy or a custom_verifyFp as method parameters

The most interesting thing there is that I can partly see obtained data from TikTok because its print HTML page in console before the error. You can see full response here: https://gist.githubusercontent.com/kostyakoz/1d5a7339217d646f1556d9fd63bcf7a8/raw/b18f4ae5e869325bb746c48fae102c8786b43fdb/error.py

My question is why obtained data not returned and I have only error?

If you save the response as HTML and open it in a browser, you will see that there's information about the profile. I think that it's a parse problem, maybe TikTok has changed the structure.

If you search for SIGI_STATE you will see a JSON object which already contains all the information.

image

image

It's used this function to get the data:

def __extract_tag_contents(self, html):
    nonce_start = '<head nonce="'
    nonce_end = '">'
    nonce = html.split(nonce_start)[1].split(nonce_end)[0]
    j_raw = html.split(
        '<script id="__NEXT_DATA__" type="application/json" nonce="%s" crossorigin="anonymous">'
        % nonce
    )[1].split("</script>")[0]
    return j_raw

and can't find <script id="__NEXT_DATA__" type="application/json" nonce="%s" crossorigin="anonymous">' in the response.

I think that this could be the clue: https://git.raptorpond.com/henine/yt-dlp/commit/11aa91a12f95821500fa064402a3e2c046b072fb

@Daan-Grashoff
Copy link
Contributor

I've made a quick dirty fix for the meantime, works a lot better since they moved from next to sigi.

https://github.com/Daan-Grashoff/TikTok-Api

You can simply install it by using pip git+https://github.com/Daan-Grashoff/TikTok-Api.

Thanks @joseantgv for pointing out where it goes wrong!

@ju
Copy link

ju commented Jan 6, 2022

I've made a quick dirty fix for the meantime, works a lot better since they moved from next to sigi.

https://github.com/Daan-Grashoff/TikTok-Api

You can simply install it by using pip git+https://github.com/Daan-Grashoff/TikTok-Api.

Thanks @joseantgv for pointing out where it goes wrong!

Thanks for the fix! Works flawlessly, but would it be possible to return the Likes, Followers & Following count for a user?

@ghost
Copy link

ghost commented Jan 6, 2022

Thanks, but it isn't working for me. I only get 'UserModule' as error output when I run
user = api.by_username(username=username).

@botcoder-sys
Copy link

Thanks, but it isn't working for me. I only get 'UserModule' as error output when I run user = api.by_username(username=username).

Have you tried remove the API and then installing this one. It should work

@ghost
Copy link

ghost commented Jan 6, 2022

Yes, I ran python -m pip uninstall TikTokApi, changed into the new Api folder and ran python setup.py install

@akorchyn
Copy link

akorchyn commented Jan 8, 2022

I've made a quick dirty fix for the meantime, works a lot better since they moved from next to sigi.

https://github.com/Daan-Grashoff/TikTok-Api

You can simply install it by using pip git+https://github.com/Daan-Grashoff/TikTok-Api.

Thanks @joseantgv for pointing out where it goes wrong!

Thank you. It works.

@ghost
Copy link

ghost commented Jan 8, 2022

Thank you. It works.

How did you install it? I'm still getting the error.

@akorchyn
Copy link

akorchyn commented Jan 8, 2022

I just replaced TktokApi with
git+https://github.com/Daan-Grashoff/TikTok-Api
in my requirements.txt file. Afterward, I rebuild my docker image and restarted instance.

@ghost
Copy link

ghost commented Jan 8, 2022

Ok. I think that @Daan-Grashoff didn't fix the method by_username. The get_user method partly works.

@akorchyn
Copy link

akorchyn commented Jan 8, 2022

I'm using it. It works. Do you use proxy ? The method is not working after some time. TikTok bans ip after some scraping.

@ghost
Copy link

ghost commented Jan 8, 2022

No, I tried it on my home ip. I just found out that the error appears, when the username doesn't exist anymore. For now it is working again. Thx.

@akorchyn
Copy link

akorchyn commented Jan 8, 2022

Great to hear

@sedanijay
Copy link

sedanijay commented Jan 15, 2022

@joseantgv
https://github.com/Daan-Grashoff/TikTok-Api this is working but giving an error for count 1000
or in case of
api = TikTokApi.get_instance()
tiktoks = api.by_username("ford", count=1000)

error:

playwright._impl._api_types.TimeoutError: Timeout 30000ms exceeded.
=========================== logs ===========================
navigating to "https://www.tiktok.com/@redbull", waiting until "load"
============================================================
Note: use DEBUG=pw:api environment variable to capture Playwright logs.

davidteather added a commit that referenced this issue Jan 21, 2022
Initial changes for v5 of the project.

Thanks @Daan-Grashoff for some of the helpers.extract_tag_contents code from a comment on #787

Co-Authored-By: Daan Grashoff <9222025+Daan-Grashoff@users.noreply.github.com>
@Doritos464
Copy link

hello, I still have the same issue and tried almost everything and nothing works.
Any suggestions?

from TikTokApi import TikTokApi
fp = "<myfp>"
api = TikTokApi.get_instance(use_test_endpoints=True)
users = api.by_username(username="davidspam464", custom_verifyFp=fp)

The error is:
TikTokApi.exceptions.TikTokCaptchaError: TikTok blocks this request displaying a Captcha Tip: Consider using a proxy or a custom_verifyFp as method parameters

davidteather added a commit that referenced this issue Feb 14, 2022
* Initial changes for v5 of the project.

* Thanks @Daan-Grashoff for some of the helpers.extract_tag_contents code from a comment on #787

* High Level Modeling of Classes

* update tests & auto call object

* Fix package-test

* simplify info_full

* Logging Improvements

* Logging changes from #804 by @zokalo

* Improve docs

* Fix some mypy typing issues

* Move old search functions to trending & fix process_kwargs

* Private self variables

Co-authored-by: davidteather <davidteather@users.noreply.github.com>
Co-authored-by: Daan Grashoff <9222025+Daan-Grashoff@users.noreply.github.com>
Co-authored-by: Dmitriy <16061619+zokalo@users.noreply.github.com>
@davidteather
Copy link
Owner

Please try using V5.0.0 this may solve your problem

@zgadev
Copy link

zgadev commented Feb 15, 2022

Daan-Grashoff's fix was working perfect until today.. 5.0 is getting "Timeout 30000ms exceeded."

@zakrian07
Copy link

async def fetch_tiktok_data(video_url: str) -> dict:
"""Fetch TikTok video data."""
async with TikTokApi() as api:

    await api.create_sessions(proxy=[proxy_config],ms_tokens=[ms_token], num_sessions=1, sleep_after=3)

    video = api.video(proxy=proxy,url=video_url)
    data = await video.info()
    return data

if name == "main":
app.run(debug=True) do we need proxy while create session or while fetch video info ? please updat me best practices when i use proxy config it not working for create session

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests