Google身份验证器扫描时无效的条形码

Jet*_*oox 3 php qr-code google-authenticator

这是我生成qr代码的代码

public function getUrl($user, $hostname, $secret) {
      $encoder = "https://chart.googleapis.com/chart?chs=200x200&chld=M%70&cht=qr&chl=";
      $encoderURL = sprintf( "%sotpauth://totp/%s@%s",$encoder, $user, $hostname);
      $finalEncodorURL = $encoderURL . "%26secret=". urlencode($secret);
      return $finalEncodorURL;
}
Run Code Online (Sandbox Code Playgroud)

据说这会生成一个qr代码,而且确实如此.但是当我使用谷歌身份验证器应用程序扫描此代码时,它将生成一个错误

The barcode 'otpauth://totp/user4@noemail.com&secret=UOPKN6QFW3J6PW74' is not a valid authentication barcode.
Run Code Online (Sandbox Code Playgroud)

但是当使用密钥"手动输入"时,它会工作,我可以使用生成的密钥登录.

我发现互联网上的东西说我应该urlencode数据,我做了,但它仍然不会工作.

这是上面函数生成的示例url:

https://chart.googleapis.com/chart?chs=200x200&chld=M%70&cht=qr&chl=otpauth://totp/user4@noemail.com%26secret=UOPKN6QFW3J6PW74
Run Code Online (Sandbox Code Playgroud)

我错过了什么或做错了什么?

Rob*_*lor 8

似乎 Google 和 IOS Authenticator 应用程序都不喜欢 OTP QR 代码中“帐户名称”中的空格。将它们替换为 - 或 _ 之类的内容或删除它们。

Windows 手机应用程序允许空间和我 6 个月前在 Android 手机上安装的版本工作。我没有机会测试 IOS,但看起来 IOS 有问题。手动输入密钥可以正常工作,但不能扫描带有空格的二维码。


Joh*_*ann 7

只需更换&secret=?secret=(不要忘记URL编码).

此外,我不知道这是否是这种情况,但帐户名称不能有空格.如果您使用Androids进行扫描,而不是使用iPhone进行扫描,则可以正常工作(如图所示!)

  • 确认...... iOS应用程序不喜欢空格.:( (3认同)

Ter*_*den 2

您应该对传递给图表 API 的数据进行完全URL 编码。

otpauth%3A%2F%2Ftotp%2Fuser4%40noemail.com%26secret%3DUOPKN6QFW3J6PW74

这应该确保数据被正确编码。