Read a QR code and extract the data Link to heading

In our case, we want to get the TOTP URL out of a QR code. Documentation for the OTP URL format can be found here on the Yubico website.

Sample code to get the data out of a QRCode image using the OpenCV2 library is here.

This should produce some output such as the following:

otpauth://totp/Example%20Company%3AJohn.Doe%40example.com?secret=aaaabbbbcccceee1123m&issuer=Google

we can then use the PyOTP library to extract the TOTP values as needed:

import pyotp
import time

totp = pyotp.parse_uri("otpauth://totp/Example%20Company%3AJohn.Doe%40example.com?secret=aaaabbbbcccceee1123m&issuer=Google")

totp.now() # => '609257'
time.sleep(30)
totp.now() # => '924749'