d60 / twikit

Twitter API Scraper | Without an API key | Twitter Internal API | Free | Twitter scraper | Twitter Bot
https://twikit.readthedocs.io/en/latest/twikit.html
MIT License
1.5k stars 176 forks source link

DenyLoginSubtask may cause IndexError #169

Open ackness opened 3 months ago

ackness commented 3 months ago

Looks like when access flow.task_id , the response sometimes get [] in subtasks

client.py#L330


        if flow.task_id == 'DenyLoginSubtask':
            raise TwitterException(flow.response['subtasks'][0]['cta']['secondary_text']['text'])
  File "/root/xxx/twitter-api/.venv/lib/python3.12/site-packages/twikit/client/client.py", line 330, in login
    if flow.task_id == 'DenyLoginSubtask':
       │    └ <property object at 0x7fba38ad5260>
       └ <twikit.utils.Flow object at 0x7fba38818bf0>
  File "/root/xxx/twitter-api/.venv/lib/python3.12/site-packages/twikit/utils.py", line 103, in task_id
    return self.response['subtasks'][0]['subtask_id']
           │    └ {'flow_token': 'g;000000000000000000000000:-0000000000000:xxxxxxxxxxxxxxxxxxxxxxx:3', 'status': 'success', 'subtasks': []}
           └ <twikit.utils.Flow object at 0x7fba38818bf0>
isntfunny commented 3 months ago

Same here

Traceback (most recent call last):
  File "C:\Users\xxx\PycharmProjects\scraper-v2\main.py", line 27, in <module>
    asyncio.run(main())
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.240.0_x64__qbz5n2kfra8p0\Lib\asyncio\runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.240.0_x64__qbz5n2kfra8p0\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.240.0_x64__qbz5n2kfra8p0\Lib\asyncio\base_events.py", line 664, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\xxx\PycharmProjects\scraper-v2\main.py", line 14, in main
    error = await client.login(
            ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\xxx\PycharmProjects\scraper-v2\venv\Lib\site-packages\twikit\client\client.py", line 330, in login
    if flow.task_id == 'DenyLoginSubtask':
       ^^^^^^^^^^^^
  File "C:\Users\xxx\PycharmProjects\scraper-v2\venv\Lib\site-packages\twikit\utils.py", line 103, in task_id
    return self.response['subtasks'][0]['subtask_id']
           ~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range
Choo-lux commented 3 months ago

yup, X shows the login notification then the error.

changing IP worked a few times then not.

d60 commented 3 months ago

It seems that twitter has implemented a measure against automated logins. Using cookies copied from the browser should still work. Use this method until a workaround is found.

bennnym commented 3 months ago

logging in seems to work for some accounts and not others

Can you elaborate on how to do that with the cookies @d60 ? I see the set_cookies on the client, but that doesnt seem to do anything for me?

Choo-lux commented 3 months ago
client.save_cookies('ookies.json')
client.load_cookies('cookies.json')

this worked for me. But I guess after this you must remove the client.login so it uses the cookies only?

m4tyn0 commented 3 months ago
client.save_cookies('ookies.json')
client.load_cookies('cookies.json')

this worked for me. But I guess after this you must remove the client.login so it uses the cookies only?

Can you please elaborate on how did you do that? what cookies are you supposed to save? I can see some in my browser - auth_multi, auth_token and many more.

ycngmn commented 3 months ago

@m4tyn0 , https://github.com/d60/twikit/issues/163#issuecomment-2253068820

d60 commented 3 months ago

Hi everyone.

I’ve published a version that might fix the subtasks issue though I can’t say it’s fully resolved. Please try it out and let me know if it works for you.

python -m pip uninstall --y twikit
python -m pip install https://github.com/d60/twikit/archive/login_fix.zip
dongruixiao commented 3 months ago

File "/home/xxx/.local/lib/python3.12/site-packages/twikit/client/client.py", line 157, in request raise BadRequest(message, headers=response.headers) twikit.errors.BadRequest: status: 400, message: "{"errors":[{"code":366,"message":"flow name LoginFlow is currently not accessible"}]}"

d60 commented 3 months ago

I found that this error is related to the User-Agent. In my environment, I encountered the error when using a Windows UA, but I was able to log in successfully with a Mac UA. It seems that the valid User-Agent might vary depending on the account.

From version 2.1.2, you can pass user_agent parameter to the client and set the UA freely. Find a valid User-Agent for your environment and configure it accordingly.

Like this:

client = Client(
    user_agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 14_6_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15'
)
DuffyAdams commented 3 months ago

i appreciate the help. After all that i'm currently getting

client = Client(
         ^^^^^^^

File "C:\Users\Duffy\AppData\Roaming\Python\Python311\site-packages\twikit\client\client.py", line 93, in init self.http = AsyncClient(proxy=proxy, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: AsyncClient.init() got an unexpected keyword argument 'user_agent'

d60 commented 3 months ago

@DuffyAdams update twijit to the latest version first.

python -m pip install -U twikit
EdmondTabaku commented 3 months ago

File "/home/xxx/.local/lib/python3.12/site-packages/twikit/client/client.py", line 157, in request raise BadRequest(message, headers=response.headers) twikit.errors.BadRequest: status: 400, message: "{"errors":[{"code":366,"message":"flow name LoginFlow is currently not accessible"}]}"

I am getting the same error, even after adding the user_agent:

client = Client(
   user_agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 14_6_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15',
   language='en-US',
   proxy=proxy
)

await client.login(
   auth_info_1=account.screen_name,
   auth_info_2=account.email,
   password=account.password
)
0x5h31d0n commented 2 months ago

Same issue

caronies commented 2 months ago

[Running] python -u "c:\Users\Nikhil\Downloads\twittermonitor.py" Traceback (most recent call last): File "c:\Users\Nikhil\Downloads\twittermonitor.py", line 4, in from twikit import Client File "C:\Users\Nikhil\AppData\Local\Programs\Python\Python311\Lib\site-packages\twikit__init__.py", line 21, in from .utils import build_query File "C:\Users\Nikhil\AppData\Local\Programs\Python\Python311\Lib\site-packages\twikit\utils.py", line 6, in from httpx import AsyncHTTPTransport ImportError: cannot import name 'AsyncHTTPTransport' from 'httpx' (C:\Users\Nikhil\AppData\Local\Programs\Python\Python311\Lib\site-packages\httpx__init__.py)

[Done] exited with code=1 in 1.753 seconds

d60 commented 2 months ago

@caronies update httpx to the latest version

d60 commented 1 month ago

Hello everyone. If you're still encountering the error flow name LoginFlow is currently not accessible, try setting the following user agent. Mozilla/5.0 (platform; rv:geckoversion) Gecko/geckotrail Firefox/firefoxversion In my case, any user agent generated by fake_useragent didn’t work, but for some reason, when I set the user agent, I was able to log in successfully. Please let me know if this works for you.

axsddlr commented 1 month ago

Hello everyone. If you're still encountering the error flow name LoginFlow is currently not accessible, try setting the following user agent. Mozilla/5.0 (platform; rv:geckoversion) Gecko/geckotrail Firefox/firefoxversion In my case, any user agent generated by fake_useragent didn’t work, but for some reason, when I set the user agent, I was able to log in successfully. Please let me know if this works for you.

nope

BillZong commented 1 month ago

Hello everyone. If you're still encountering the error flow name LoginFlow is currently not accessible, try setting the following user agent. Mozilla/5.0 (platform; rv:geckoversion) Gecko/geckotrail Firefox/firefoxversion In my case, any user agent generated by fake_useragent didn’t work, but for some reason, when I set the user agent, I was able to log in successfully. Please let me know if this works for you.

We've tried this, and bad news for current twitter login flow changes. The only way to make it passed is to postpone query id from subtasks response to the last stage, till all flows including LoginTwoFactorAuthChallenge done. @d60 Would be nice to fix it in time, or should I provide a PR?

EyalShechtman commented 1 month ago

It seems like every time I try to use the client.login(), Twitter is detecting a suspicious log in, and prompts me to answer security questions/enter verification code. Is there a fix yet? I can try to help if you got the PR approved.

Atlantropaz commented 4 weeks ago

Mozilla/5.0 (platform; rv:geckoversion) Gecko/geckotrail Firefox/firefoxversion

Not working on my end.

TheNoobiCat commented 2 weeks ago

Hello everyone. If you're still encountering the error flow name LoginFlow is currently not accessible, try setting the following user agent. Mozilla/5.0 (platform; rv:geckoversion) Gecko/geckotrail Firefox/firefoxversion In my case, any user agent generated by fake_useragent didn’t work, but for some reason, when I set the user agent, I was able to log in successfully. Please let me know if this works for you.

surprisingly this worked for me..? very strange. I must say, it does seem unstable too