问题
I was looking for a Python Time-based OTP library when I found PyOtp. I scanned the sample QR-Code
using Google Authenticator and ran the associated sample code
import pyotp
totp = pyotp.TOTP("JBSWY3DPEHPK3PXP")
print("Current OTP:", totp.now())
However, the codes on my phones and the codes which the application generates do not match. I also tried another library (SpookyOTP) and the codes also did not match. Generating my own key with PyOTP, instead of using the sample one, also did not make it work.
Obviously I am doing something wrong. The only thing that comes to my mind is that the two devices (my pc and my phone) somehow are not using the same timestamp for the generation of the code. But both devices show the same time on the clock.
回答1:
These code are time based - are you scanning them on the Google Authenticator app on your device?
You have to make sure the time on both your phone and server (where your are hosting the script) are synced.
To sync the Google Authenticator App:
My Google Authenticator codes aren’t working (Android) This might be because the time on your Google Authenticator app is not synced correctly.
To make sure that you have the correct time: https://support.google.com/accounts/answer/185834?hl=en
Go to the main menu on the Google Authenticator app Tap More and then Settings. Tap Time correction for codes Tap Sync now On the next screen, the app will confirm that the time has been synced, and you should now be able to use your verification codes to sign in. The sync will only affect the internal time of your Google Authenticator app, and will not change your device’s Date & Time settings.
To Sync your server you need to use NTP to sync(I am assuming Ubuntu but its similar for other OS`s):
https://www.digitalocean.com/community/tutorials/how-to-set-up-time-synchronization-on-ubuntu-16-04
Installation To install ntpd, from a terminal prompt enter:
sudo apt install ntp
Configuration Edit /etc/ntp.conf to add/remove server lines. By default these servers are configured:
# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
server 0.ubuntu.pool.ntp.org
server 1.ubuntu.pool.ntp.org
server 2.ubuntu.pool.ntp.org
server 3.ubuntu.pool.ntp.org
After changing the config file you have to reload the ntpd:
sudo systemctl reload ntp.service
来源:https://stackoverflow.com/questions/46978522/generated-codes-do-not-match-with-pyotp-sample