Giter Site home page Giter Site logo

remove cookies before login about twikit HOT 6 CLOSED

ShoGinn avatar ShoGinn commented on May 29, 2024
remove cookies before login

from twikit.

Comments (6)

itsManeka avatar itsManeka commented on May 29, 2024 2

I understand now! In fact, the suggested correction has already been uploaded.

from twikit.

d60 avatar d60 commented on May 29, 2024 2

I forgot to close this issue. Sorry for the confusion!
@ShoGinn @itsManeka

from twikit.

ShoGinn avatar ShoGinn commented on May 29, 2024 2

I think that any dialog/examination of code is beneficial and shows that your efforts are being acknowledged so apologies are unnecessary!

from twikit.

ShoGinn avatar ShoGinn commented on May 29, 2024 1

@itsManeka @d60

That's hilarious ;) I'll have to pick on @d60 for not closing this, then!

I'll close it with this comment!!!

Good find @itsManeka

from twikit.

itsManeka avatar itsManeka commented on May 29, 2024

Just use client.set_cookies({}, True)
The first arg is te cookies (se {} to avoid) and the second arg is a flag to clear cookies.

from twikit.

ShoGinn avatar ShoGinn commented on May 29, 2024

@itsManeka

I understand there is a way to clear cookies.

Lets use this scenario:

First Program Run:
login, save cookies.

Second Program Run:
note that cookies are saved and use saved cookies (using set_cookies)
get_tweets etc. works fine

Third Program Run:
note that cookies are saved. Use saved cookies (using set_cookies)
get_tweets throws an error for Unauthorized


This is where you are saying to run, then
.set_cookies(clear_cookies=true)

Real case.
.login() and cookies are used anyway... so you have to.

This is my current code:

def login_with_credentials(twikit_client: Client, mongodb_client: MongoDBClient, app_settings: Settings) -> bool:
    """Login to X using the provided credentials."""
    # Check to make sure the authentication information is provided
    if not app_settings.x_username or not app_settings.x_email or not app_settings.x_password.get_secret_value():
        raise ValueError("X credentials are missing or incomplete.")
    logger.info("Logging in to X...")
    twikit_client.http.client.cookies.clear()  # TODO: Remove this once the twikit library is updated
    try:
        twikit_client.login(
            auth_info_1=app_settings.x_username,
            auth_info_2=app_settings.x_email,
            password=app_settings.x_password.get_secret_value(),
        )
        logger.info("Successfully logged in to X.")
        store_cookies(mongodb_client, twikit_client)
        return True
    except Exception as e:
        logger.error(f"Error logging in to X: {e}")
        logger.error("Please check your X credentials and run the program again.")
        return False

Because the .login() uses the HTTP engine, and if you have set cookies, it will always attempt to use cookies.

I hope I have explained why clearing cookies in .login() is essential because of the HTTP engine's nature.

from twikit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.