生成的代码与 PyOTP 示例不匹配

Generated codes do not match with PyOTP sample

当我找到 PyOtp 时,我正在寻找 Python 基于时间的 OTP 库。我扫描了样本二维码

使用 Google 验证器和 运行 相关的示例代码

import pyotp
totp = pyotp.TOTP("JBSWY3DPEHPK3PXP")
print("Current OTP:", totp.now())

但是,我的phones 上的代码与应用程序生成的代码不匹配。我还尝试了另一个库 (SpookyOTP),但代码也不匹配。使用 PyOTP 生成我自己的密钥,而不是使用示例密钥,也无法正常工作。

显然我做错了什么。我唯一想到的是,这两个设备(我的电脑和我的 phone)不知何故没有使用相同的时间戳来生成代码。但是两个设备在时钟上显示相同的时间。

这些代码是基于时间的 - 您是否在设备上的 Google 身份验证器应用程序上扫描它们?

您必须确保 phone 和服务器(您托管脚本的服务器)上的时间同步。

要同步 Google 身份验证器应用程序:

我的 Google 验证码无效 (Android) 这可能是因为您的 Google 身份验证器应用上的时间未正确同步。

为确保您的时间正确: 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.

要同步您的服务器,您需要使用 NTP 进行同步(我假设 Ubuntu 但其他 OS`s 类似):

https://www.digitalocean.com/community/tutorials/how-to-set-up-time-synchronization-on-ubuntu-16-04

安装 要安装 ntpd,请在终端提示符下输入:

sudo apt install ntp

配置 将 /etc/ntp.conf 编辑为 add/remove 服务器行。默认配置这些服务器:

# 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

更改配置文件后,您必须重新加载 ntpd:

sudo systemctl reload ntp.service