Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Two-Factor Authentication (2FA) Login Fails Despite Correct Code #2109

Open
Germanized opened this issue Jan 18, 2025 · 0 comments
Open
Labels
bug Something isn't working

Comments

@Germanized
Copy link

Try Instagrapi SaaS with a free trial https://hikerapi.com/p/5GBWznd3

Describe the bug
When attempting to log in to Instagram with an account that has Two-Factor Authentication (2FA) enabled, the instagrapi library fails to complete the login process even after the correct verification code is provided.

To Reproduce

  1. Use the instagrapi library to attempt login with a username and password for an account that has 2FA enabled.
  2. When prompted for the 2FA code, input the correct verification code from the authenticator app.
  3. The login fails with the message:
    • "Login failed: Two-factor authentication required (you did not provide verification_code for login method)"

Expected behavior
The library should accept the provided 2FA verification code and log in successfully.

Actual Behavior

Despite entering the correct verification code, the login fails with the error mentioned above.

Example Code

Here is the relevant section of code where the login is handled:

def handle_2fa():
    """Handle login for accounts with 2FA enabled"""
    global api
    username = Prompt.ask("Enter your Instagram username")
    password = Prompt.ask("Enter your Instagram password", password=True)
    verification_code = Prompt.ask("[bold cyan]Enter the 2FA verification code from your authenticator app")

    try:
        api = Client()
        # Attempt to log in using the username and password
        api.login(username, password)
        
        # If the login triggers 2FA, the following logic handles it
        if api.two_factor_enabled():
            api.two_factor_login(verification_code)  # Pass the verification code explicitly

        # Verify successful login
        console.print(Panel(
            "[green]Login successful with 2FA verification!",
            border_style="green",
            box=box.ROUNDED
        ))
        return api
    except Exception as e:
        console.print(Panel(
            f"[red]Login failed: {str(e)}",
            border_style="red",
            box=box.ROUNDED
        ))
        return None

Desktop (please complete the following information):

  • OS: Windows 10.0.22631
  • Python version: 3.12.4
  • instagrapi version: instagrapi 2.1.3
  • moveipy version: moveipy not installed or command not found
  • imagemagick version: convert version check failed

Additional context
Add any other context about the problem here.

@Germanized Germanized added the bug Something isn't working label Jan 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant