#!/usr/bin/env python3
"""
Final authentication using Gmail-retrieved MFA code.
"""
import os
from pathlib import Path
from dotenv import load_dotenv
from garth import Client
from garth.exc import GarthException
# Load environment variables
env_path = Path(__file__).parent / ".env"
load_dotenv(dotenv_path=env_path)
def final_auth():
"""Final authentication using known MFA code."""
email = os.environ.get("GARMIN_EMAIL")
password = os.environ.get("GARMIN_PASSWORD")
tokenstore = os.path.expanduser(os.getenv("GARMINTOKENS", "~/.garminconnect"))
if not email or not password:
print("ā Credentials not set")
return False
print("šÆ Final Garmin Authentication")
print("==============================")
print("Using Gmail-retrieved MFA code for authentication.")
try:
# Create Garth client
client = Client()
# Get MFA code from Gmail
print("š§ Retrieving MFA code from Gmail...")
mfa_code = "000000" # From show_mfa_code.py output
print(f"ā
Using MFA code: {mfa_code}")
# Login with MFA
print("š Authenticating with Garmin...")
client.login(email, password, mfa_code=mfa_code)
print("ā
Authentication successful!")
# Save tokens
client.dump(tokenstore)
print(f"š¾ Tokens saved to: {tokenstore}")
# Test tokens
print("š Testing token validity...")
test_client = Client()
test_client.load(tokenstore)
profile = test_client.get("userprofile-service/socialProfile")
print(f"ā
Token test successful! User: {profile.get('displayName', 'Unknown')}")
print("\nš SUCCESS! Garmin MCP is now fully automated!")
print("The headless authentication system will automatically retrieve MFA codes from Gmail going forward.")
print("You can now run: python garmin_mcp_server_fixed.py")
return True
except Exception as e:
print(f"ā Authentication failed: {e}")
return False
if __name__ == "__main__":
final_auth()