【问题标题】:Generated codes do not match with PyOTP sample生成的代码与 PyOTP 示例不匹配
【发布时间】:2017-10-27 15:25:18
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: python one-time-password google-authenticator


    【解决方案1】:

    这些代码是基于时间的 - 您是在设备上的 Google Authenticator 应用上扫描它们吗?

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

    要同步 Google Authenticator 应用程序:

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

    为了确保您有正确的时间: https://support.google.com/accounts/answer/185834?hl=en

    转到 Google Authenticator 应用程序的主菜单 点击更多,然后 设置。点击代码的时间校正 点击立即同步 在下一个 屏幕,应用程序将确认时间已同步,您 现在应该可以使用您的验证码登录了。同步 只会影响您的 Google Authenticator 应用程序的内部时间, 并且不会更改您设备的日期和时间设置。

    要同步您的服务器,您需要使用 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
    

    【讨论】:

      猜你喜欢
      • 2020-03-03
      • 2011-12-11
      • 2016-03-04
      • 2018-08-13
      • 1970-01-01
      • 2018-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多