twikit icon indicating copy to clipboard operation
twikit copied to clipboard

Automated login asks for 'Confirmation Code'.

Open Rohit7Patil opened this issue 6 months ago • 2 comments

I am using twikit to automate twitter login and crawl a set of handles or hashtags.

The automated login is asking for a confirmation code.

Provide the ability to send the confirmation code sent to the email to a frontend, so that the user can input it on the frontend rather than the terminal on the backend.

Note: the frontend is a custom interface I'm developing.

-- OR --

If there is something wrong with the way I'm using twikit to automate twitter login, please let me know, below is the login code.

async def login(cookie_file: str = COOKIE_FILE) -> Client: client = Client(language="en-US")

if os.path.exists(cookie_file):
    try:
        client.load_cookies(cookie_file)
        await client.search_tweet("test", product="Latest", count=1)
        logger.info(f"Loaded cookies from {cookie_file}")
        return client
    except Exception:
        logger.info(f"Failed to load cookies from {cookie_file}; deleting them.")
        os.remove(cookie_file)

cfg = ConfigParser()
cfg.read(os.path.join(os.path.dirname(__file__), "config_j.ini"))
user = os.getenv("TWITTER_USERNAME") or cfg.get("X", "username", fallback=None)
email = os.getenv("TWITTER_EMAIL") or cfg.get("X", "email", fallback=None)
pwd = os.getenv("TWITTER_PASSWORD") or cfg.get("X", "password", fallback=None)

logger.info(f"Logging in with user: {user}")
try:
    await client.login(auth_info_1=user, auth_info_2=email, password=pwd)
    client.save_cookies(cookie_file)
    logger.info(f"Saved cookies to {cookie_file}")
except TwitterException:
    logger.error("Login failed!")
    raise

return client

Rohit7Patil avatar Jun 29 '25 15:06 Rohit7Patil

There's nothing wrong with the Way you're using twikit. As it's more of a twitter thing to use verification means like this to curtail bots. A custom solution would be to automate the associated email to read and input the code automatically.

princewillingoo avatar Jul 06 '25 19:07 princewillingoo

Hi @princewillingoo,

thanks for the suggestion, I'm fairly new to this library and am using python as backend and react as frontend.

my goal is such that the user can input the confirmation code on the frontend and the twikit client should be able to accept it on the backend.

if you can please share any code snippets, I would really appreciate it.

Rohit7Patil avatar Jul 21 '25 11:07 Rohit7Patil