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.19k stars 133 forks source link

DenyLoginSubtask may cause IndexError #169

Open ackness opened 1 month ago

ackness commented 1 month 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 1 month 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 1 month ago

yup, X shows the login notification then the error.

changing IP worked a few times then not.

d60 commented 1 month 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 1 month 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 1 month 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 1 month 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 1 month ago

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

d60 commented 1 month 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 1 month 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 1 month 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 1 month 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 1 month ago

@DuffyAdams update twijit to the latest version first.

python -m pip install -U twikit
EdmondTabaku commented 3 weeks 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 1 week ago

Same issue

caronies commented 1 week 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 1 week ago

@caronies update httpx to the latest version