How to use PyOTP to generate TOTP token from othpauth:// URL

You can use the following code to generate a TOTP token from an otpauth:// URL using the PyOTP library:

import pyotp
from urllib.parse import urlparse, parse_qs

# Sample otpauth URL (replace with your actual URL)
otpauth_url = "otpauth://totp/..."

# Parse the otpauth URL to extract the secret
parsed_url = urlparse(otpauth_url)
query_params = parse_qs(parsed_url.query)

# Extract the secret from the URL query parameters
secret = query_params['secret'][0]

# Create a TOTP object using the secret
totp = pyotp.TOTP(secret)

# Generate the current TOTP token
token = totp.now()

print(f"TOTP Token: {token}")

This will print, for example:

TOTP Token: 312402