【发布时间】:2019-01-30 07:57:17
【问题描述】:
所以我在使用 google 身份验证器和我的 PHP 时遇到了问题。
所以我使用这个库来生成二维码:https://github.com/PHPGangsta/GoogleAuthenticator
因此,当我使用我的用户名生成代码时,它可以正常工作。我得到了一些类似的东西: otpauth://totp/username?secret=aCodeInBase32&issuer=Mysite
就我而言,它是: otpauth://totp/NoahNok?secret=aCodeInBase32&issuer=Mysite
但是,在为任何其他用途执行此操作时,我在 Google Authenticator 应用程序上收到无效令牌错误。不管我放什么其他东西,我似乎总是收到这个错误,但它对我的帐户来说很好。
例如,这个不起作用:otpauth://totp/Test?secret=KRSX&issuer=MySite
有什么明显的我做错了吗?
代码即时使用: 获取数据之前的一些查询
$g = new PHPGangsta_GoogleAuthenticator();
include("Base32.php");
$secret = substr(Base32::encode($username),0,-4);
echo $g->getQRCodeGoogleUrl($username, $secret, "MySite");
生成二维码网址
public function getQRCodeGoogleUrl($name, $secret, $title = null, $params = array())
{
$width = !empty($params['width']) && (int) $params['width'] > 0 ? (int) $params['width'] : 200;
$height = !empty($params['height']) && (int) $params['height'] > 0 ? (int) $params['height'] : 200;
$level = !empty($params['level']) && array_search($params['level'], array('L', 'M', 'Q', 'H')) !== false ? $params['level'] : 'M';
$urlencoded = urlencode('otpauth://totp/'.$name.'?secret='.$secret.'');
if (isset($title)) {
$urlencoded .= urlencode('&issuer='.urlencode($title));
}
return 'https://chart.googleapis.com/chart?chs='.$width.'x'.$height.'&chld='.$level.'|0&cht=qr&chl='.$urlencoded.'';
}
【问题讨论】:
-
欢迎来到 SO。请阅读:How to create a Minimal, Complete, and Verifiable example 和 How do I ask a good question? 为了让人们更容易理解您的问题并查看发生了什么,您应该始终包含您遇到问题的代码。
-
用相关信息编辑帖子