Hello! I am making an authenticator for OTP’s in Python, but I need help with the code. I am getting an attribue error. Here is the code if that helps.
import pyotp
import pyqrcode
import tkinter as tk
from tkinter import messagebox
class AuthenticatorApp:
def __init__(self, master):
self.master = master
master.title("Authenticator App")
# Generate a random secret key and create a TOTP object
self.secret_key = pyotp.random_base32()
self.totp = pyotp.TOTP(self.secret_key)
# Generate a QR code image and convert it to a Tkinter PhotoImage object
self.qr_data = self.totp.provisioning_uri("user@example.com")
self.qr_code = pyqrcode.create(self.qr_data)
self.qr_image_data = self.qr_code.png_as_bytes(scale=5)
self.qr_image = tk.PhotoImage(data=self.qr_image_data)
# Create the user interface
self.label = tk.Label(master, text="Scan this QR code with your authenticator app:")
self.label.pack()
self.qr_label = tk.Label(master, image=self.qr_image)
self.qr_label.pack()
self.code_label = tk.Label(master, text="Enter the code from your authenticator app:")
self.code_label.pack()
self.code_entry = tk.Entry(master)
self.code_entry.pack()
self.button = tk.Button(master, text="Authenticate", command=self.authenticate)
self.button.pack()
# Start the update loop
self.update()
def update(self):
# Update the current TOTP code
self.current_code = self.totp.now()
# Schedule the next update
self.master.after(1000, self.update)
def authenticate(self):
# Get the code entered by the user
entered_code = self.code_entry.get()
if entered_code == self.current_code:
messagebox.showinfo("Success", "Authentication successful!")
else:
messagebox.showerror("Error", "Incorrect code.")
if __name__ == "__main__":
root = tk.Tk()
app = AuthenticatorApp(root)
root.mainloop()
Thank you!