Wie man PyOTP verwendet, um ein TOTP-Token aus einer otpauth://-URL zu generieren

Sie können den folgenden Code verwenden, um ein TOTP-Token aus einer otpauth://-URL mit der PyOTP-Bibliothek zu generieren:

pyotp_from_otpauth.py
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}")

Dies wird beispielsweise ausgeben:

totp_token_output.txt
TOTP Token: 312402

Check out similar posts by category: Python, Cryptography