d60 / twikit

Twitter API Scraper | Without an API key | Twitter Internal API | Free | Twitter scraper | Twitter Bot

Home Page:https://twikit.readthedocs.io/en/latest/twikit.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

twikit.errors.BadRequest: status: 400

radian462 opened this issue · comments

(I use DeepL translation)
I was curious about this library and tried this library.
It worked fine a few times with this code, but then it stopped working with this error.
Please let me know how I can fix it.

import os

client = Client('ja')
client.load_cookies('cookies.json')
client.login(
    auth_info_1= os.getenv("email"),
    password= os.getenv("password")
)

client.save_cookies('cookies.json')

tweet = client.get_tweet_by_id("#########")
print(tweet)
  File "/home/runner/twikit/main.py", line 6, in <module>
    client.login(
  File "/home/runner/twikit/.pythonlibs/lib/python3.10/site-packages/twikit/client.py", line 195, in login
    flow.execute_task({
  File "/home/runner/twikit/.pythonlibs/lib/python3.10/site-packages/twikit/utils.py", line 388, in execute_task
    response = self._client.http.post(
  File "/home/runner/twikit/.pythonlibs/lib/python3.10/site-packages/twikit/http.py", line 54, in post
    return self.request('POST', url, **kwargs)
  File "/home/runner/twikit/.pythonlibs/lib/python3.10/site-packages/twikit/http.py", line 32, in request
    raise BadRequest(message, headers=response.headers)
twikit.errors.BadRequest: status: 400, message: "{"errors":[{"code":366,"message":"Missing data."}]}"
commented

@radian462
Set the username or phone number to auth_info_2. Also, you shouldn't call the login method multiple times to prevent your account from being locked. Here's modified code:

client = Client('ja')
if os.path.exists('cookies.json'):
    client.load_cookies('cookies.json')
else:
    client.login(
        auth_info_1= os.getenv("email"),
        auth_info_2= os.getenv("username"),  # Or phone number
        password= os.getenv("password")
    )
    client.save_cookies('cookies.json')

tweet = client.get_tweet_by_id("#########")
print(tweet)

Thank you very much. My problem has been solved.