Php 使用Google_Oauth2Service API进行身份验证
我有以下登录代码: JS登录控制器:Php 使用Google_Oauth2Service API进行身份验证,php,google-authentication,Php,Google Authentication,我有以下登录代码: JS登录控制器: token = window.localStorage.getItem("token"); Ext.php.Users.login(token, function(n) { if (Ext.isString(n)) window.location = n; else if (n != null) { app.user = user; r.add({ id: 0, email: t.email,
token = window.localStorage.getItem("token");
Ext.php.Users.login(token, function(n) {
if (Ext.isString(n)) window.location = n;
else if (n != null) {
app.user = user;
r.add({
id: 0,
email: t.email,
password: t.password,
roles: user.Roles,
defaultrole: user.DefaultRole
});
e.loadMainView("userHome");
}
});
PHP登录函数:
public function login($token) {
$client = new Google_Client();
$oauth2 = new Google_Oauth2Service($client);
if (!empty($token)) {
$token = base64_decode($token);
$client->setAccessToken($token);
if (!$client->isAccessTokenExpired()) {
$user = $oauth2->userinfo->get();
$email = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
$u = $this->getByEmail($email);
if ($u != null) return $u;
}
$this->logout($token);
}
$authUrl = $client->createAuthUrl();
return $authUrl;
}
在我的google/config.php文件中:
...
'oauth2_redirect_uri' => getLoginCallBackUrl(),
...
该函数位于同一文件中:
function getLoginCallBackUrl() {
$pageURL = 'http';
//if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return substr($pageURL,0,strpos($pageURL,'/api/') + 5) .'login_callback.php';
}
基本上,在我尝试登录之后,我被要求进行谷歌认证。我将其传递给测试用户,并将其重定向回我的web应用程序:但令牌虽然最初已加载,但在应用程序完全加载时会被丢弃,我无法登录。再次尝试登录只会重复google身份验证,但没有成功
我做错了什么
编辑:我成功了。上面的代码是正确的。模式更改后,我的一个字段出现了拼写错误。我将留下问题的细节,希望代码可以帮助某人,某处。正如我在编辑中所写,上面的代码实际上是正确的,尽管这里没有显示一些控制器登录JS代码。该问题是架构更改后的字段输入错误。您可能希望回答自己的问题,将其标记为已回答,而不是将已解决的问题写入标题中。谢谢你的提醒。我会尽快接受的。