我使用作曲家下载了Hybrid Auth,并将index.php和config.php文件复制到了root。然后,我将index.php文件重命名为hybrid.php文件。我只是想从Facebook获取用户的电子邮件。
Auth.php
抛出此异常:
致命错误:未捕获的异常:验证失败!脸书 返回了无效的用户ID。在C:\ xampp \ htdocs \ hybrid中 auth \ vendor \ hybridauth \ hybridauth \ hybridauth \ Hybrid \ Auth.php:153堆栈 跟踪:#0 C:\ xampp \ htdocs \ hybrid auth \ vendor \ hybridauth \ hybridauth \ hybridauth \ Hybrid \ Auth.php(39): Hybrid_Auth :: initialize(Array)#1 C:\ xampp \ htdocs \ hybrid auth \ index.php(6):Hybrid_Auth-> __ construct('config.php')#2 {main} 抛出C:\ xampp \ htdocs \ hybrid 在线上的auth \ vendor \ hybridauth \ hybridauth \ hybridauth \ Hybrid \ Auth.php 153
混合身份验证调试文件:
我创建了一个新的index.php文件,如下所示:
<?php
require 'vendor/autoload.php';
$model = new \Model\App_Model();
$hybridInstance = new Hybrid_Auth('config.php');
$session_identifier = Hybrid_Auth::storage()->get( 'user' );
if (is_null( $session_identifier )) {
echo "redirect to login";
try {
$adapter = $hybridInstance->authenticate( ucwords('facebook') );
$user_profile = $adapter->getUserProfile();
if (empty( $user_profile )) {
echo "/login/?err=1";
}
$identifier = $user_profile->identifier;
if ($model->identifier_exists( $identifier )) {
$model->login_user( $identifier );
echo "welcome";
} else {
$register = $model->register_user(
$identifier,
$user_profile->email,
$user_profile->firstName,
$user_profile->lastName,
$user_profile->photoURL
);
if ($register) {
$model->login_user( $identifier );
echo "register and welcome";
}
}
} catch ( Exception $e ) {
echo $e->getMessage();
}
}
我的App_Model.php
<?php
namespace Model;
class App_Model {
private $conn;
public function __CONSTRUCT() {
//initialize database
include('db.php');
$this->conn = $db;
}
/**
* Check if a HybridAuth identifier already exists in DB
*
* @param int $identifier
*
* @return bool
*/
public function identifier_exists($identifier)
{
try {
$sql = 'SELECT identifier FROM users';
$query = $this->conn->query($sql);
$result = $query->fetchAll(\PDO::FETCH_COLUMN, 0);
return in_array($identifier, $result);
} catch ( \PDOException $e ) {
die( $e->getMessage() );
}
}
/**
* Save users record to the database.
*
* @param string $identifier user's unique identifier
* @param string $email
* @param string $first_name
* @param string $last_name
* @param string $avatar_url
*
* @return bool
*/
public function register_user( $identifier, $email, $first_name, $last_name, $avatar_url )
{
try {
$sql = "INSERT INTO users (identifier, email, first_name, last_name, avatar_url) VALUES (:identifier, :email, :first_name, :last_name, :avatar_url)";
$query = $this->conn->prepare($sql);
$query->bindValue(':identifier', $identifier);
$query->bindValue(':email', $email);
$query->bindValue(':first_name', $first_name);
$query->bindValue(':last_name', $last_name);
$query->bindValue(':avatar_url', $avatar_url);
return $query->execute();
} catch (\PDOException $e) {
return $e->getMessage();
}
}
/**
* Create user login session
*
* @param int $identifier
*/
public function login_user($identifier)
{
\Hybrid_Auth::storage()->set('user', $identifier);
}
/** Destroy user login session */
public function logout_user()
{
\Hybrid_Auth::storage()->set( 'user', null );
}
/**
* Return user's first name.
*
* @param int $identifier
*
* @return string
*/
public function getFirstName( $identifier )
{
if ( ! isset( $identifier )) {
return;
}
$query = $this->conn->prepare( "SELECT first_name FROM users WHERE identifier = :identifier" );
$query->bindParam( ':identifier', $identifier );
$query->execute();
$result = $query->fetch( \PDO::FETCH_NUM );
return $result[0];
}
/**
* Return user's last name.
*
* @param int $identifier
*
* @return string
*/
public function getLastName( $identifier )
{
if ( ! isset( $identifier )) {
return;
}
$query = $this->conn->prepare( "SELECT last_name FROM users WHERE identifier = :identifier" );
$query->bindParam( ':identifier', $identifier );
$query->execute();
$result = $query->fetch( \PDO::FETCH_NUM );
return $result[0];
}
/**
* Return user's email address
*
* @param int $identifier
*
* @return string
*/
public function getEmail( $identifier )
{
if ( ! isset( $identifier )) {
return;
}
$query = $this->conn->prepare( "SELECT email FROM users WHERE identifier = :identifier" );
$query->bindParam( ':identifier', $identifier );
$query->execute();
$result = $query->fetch( \PDO::FETCH_NUM );
return $result[0];
}
/**
* Return the URL of user's avatar
*
* @param int $identifier
*
* @return string
*/
public function getAvatarUrl( $identifier )
{
if ( ! isset( $identifier )) {
return;
}
$query = $this->conn->prepare( "SELECT avatar_url FROM users WHERE identifier = :identifier" );
$query->bindParam( ':identifier', $identifier );
$query->execute();
$result = $query->fetch( \PDO::FETCH_NUM );
return $result[0];
}
}
我的config.php文件:
<?php
/**
* HybridAuth
* http://hybridauth.sourceforge.net | http://github.com/hybridauth/hybridauth
* (c) 2009-2014, HybridAuth authors | http://hybridauth.sourceforge.net/licenses.html
*/
// ----------------------------------------------------------------------------------------
// HybridAuth Config file: http://hybridauth.sourceforge.net/userguide/Configuration.html
// ----------------------------------------------------------------------------------------
return
array(
"base_url" => "http://localhost/hybrid%20auth/hybrid.php",
"providers" => array (
// openid providers
"OpenID" => array (
"enabled" => false
),
"Yahoo" => array (
"enabled" => false,
"keys" => array ( "key" => "", "secret" => "" ),
),
"AOL" => array (
"enabled" => false
),
"Google" => array (
"enabled" => false,
"keys" => array ( "id" => "", "secret" => "" ),
),
"Facebook" => array (
"enabled" => true,
"keys" => array ( "id" => "my_app_id_was_here", "secret" => "my_app_secret_was_here" ),
'scope' => 'email',
"trustForwarded" => true
),
"Twitter" => array (
"enabled" => false,
"keys" => array ( "key" => "", "secret" => "" )
),
// windows live
"Live" => array (
"enabled" => false,
"keys" => array ( "id" => "", "secret" => "" )
),
"LinkedIn" => array (
"enabled" => false,
"keys" => array ( "key" => "", "secret" => "" )
),
"Foursquare" => array (
"enabled" => false,
"keys" => array ( "id" => "", "secret" => "" )
),
),
// If you want to enable logging, set 'debug_mode' to true.
// You can also set it to
// - "error" To log only error messages. Useful in production
// - "info" To log info and error messages (ignore debug messages)
"debug_mode" => true,
// Path to file writable by the web server. Required if 'debug_mode' is not false
"debug_file" => "debug",
);