我将 HybridAuth2 与 CakePHP 一起使用,我的控制器功能为
public function loginwith($provider) {
// $this->autoRender = false;
require_once( WWW_ROOT . 'hybridauth/Hybrid/Auth.php' );
$hybridauth_config = array(
"base_url" => 'http://' . $_SERVER['HTTP_HOST'] . $this->base . "/hybridauth/", // set hybridauth path
"providers" => array(
"Google" => array(
"enabled" => true,
"keys" => array("id" => "clientID", "secret" => "clientSecret")
)
)
);
try {
// create an instance for Hybridauth with the configuration file path as parameter
$hybridauth = new Hybrid_Auth($hybridauth_config);
// try to authenticate the selected $provider
$adapter = $hybridauth->authenticate($provider);
// grab the user profile
$user_profile = $adapter->getUserProfile();
debug($user_profile);
} catch (Exception $e) {
// Display the recived error
switch ($e->getCode()) {
case 0 : $error = "Unspecified error.";
break;
case 1 : $error = "Hybriauth configuration error.";
break;
case 2 : $error = "Provider not properly configured.";
break;
case 3 : $error = "Unknown or disabled provider.";
break;
case 4 : $error = "Missing provider application credentials.";
break;
case 5 : $error = "Authentification failed. The user has canceled the authentication or the provider refused the connection.";
break;
case 6 : $error = "User profile request failed. Most likely the user is not connected to the provider and he should to authenticate again.";
$adapter->logout();
break;
case 7 : $error = "User not connected to the provider.";
$adapter->logout();
break;
}
// well, basically you should not display this to the end user, just give him a hint and move on..
$error .= "Original error message: " . $e->getMessage();
$error .= "Trace: " . $e->getTraceAsString();
$this->set('error', $error);
}
}
HybridAuth 库位于 app 文件夹下的 webroot 文件夹中
但是,我收到了案例 6中的错误,即
case 6 : $error = "用户配置文件请求失败。很可能用户没有连接到提供者,他应该再次进行身份验证。";
确切的错误信息是Undefined variable: adapter [APP\Controller\UsersController.php, line 177]
指向 CASE 6
任何人都可以建议发生了什么。我无法调试这个。
PS 这只发生在谷歌登录
问候, 枪