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

标签 python one-time-password google-authenticator

当我发现 PyOtp 时,我正在寻找基于 Python 时间的 OTP 库。 。我扫描了示例二维码

enter image description here

使用 Google Authenticator 并运行关联的示例代码

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

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

显然我做错了什么。我唯一想到的是两个设备(我的电脑和手机)在某种程度上没有使用相同的时间戳来生成代码。但两个设备在时钟上显示相同的时间。

最佳答案

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

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

要同步 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,但与其他操作系统类似):

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

安装 要安装 ntpd,请从终端提示符处输入:

sudo apt install ntp

配置 编辑/etc/ntp.conf 以添加/删除服务器线路。默认情况下,这些服务器已配置:

# 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

关于python - 生成的代码与 PyOTP 示例不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46978522/

相关文章:

Python:在元组列表中,找到下一个元组的值时检索前一个元组的键?

c - 在 C 中实现 hmac sha1

google-plus - Android Google Auth 登录获取 ID token handleSignInResult :false

ios - 适用于 iOS 的 Google 身份验证器丢失文件

python - ImportError异常值: No module named forms

python - python中round()函数舍入变化的确切临界值是多少?

python - 根据数字序列预测 5 个下一个值

java - 谷歌 OTP 生成 Java

textfield - SwiftUI TextField 建议没有 textContentType 的 OTP 代码

php - Composer - 请求的包 phpgangsta/googleauthenticator 1.0.1 作为 [dev-master] 存在,但这些被您的约束拒绝