我花了最后2天尝试使用Goggle应用引擎将facebook登录添加到我的网站,我在文档中看到需要启用curl并且可以使用php.ini文件完成,但是尽管如下说明我仍然无法设置facebook会话以便我可以在重定向后登录用户,下面是我的代码,任何帮助都会有很长的路要走。
的app.yaml
application: appname
version: 1
runtime: php55
api_version: 1
handlers:
- url: /images
static_dir: images
- url: /assets
static_dir: assets
- url: /includes
static_dir: includes
- url: /http/.*
script: http.php
- url: /.*
script: index.php
的php.ini
extension = "curl.so"
的index.php
require_once ('/includes/php_functions/lib/Facebook/FacebookSession.php');
require_once ('/includes/php_functions/lib/Facebook/FacebookRequest.php');
require_once ('/includes/php_functions/lib/Facebook/FacebookResponse.php');
require_once ('/includes/php_functions/lib/Facebook/FacebookSDKException.php');
require_once ('/includes/php_functions/lib/Facebook/FacebookRequestException.php');
require_once ('/includes/php_functions/lib/Facebook/FacebookRedirectLoginHelper.php');
require_once ('/includes/php_functions/lib/Facebook/FacebookAuthorizationException.php');
require_once ('/includes/php_functions/lib/Facebook/GraphObject.php');
require_once ('/includes/php_functions/lib/Facebook/GraphUser.php');
require_once ('/includes/php_functions/lib/Facebook/GraphSessionInfo.php');
require_once ('/includes/php_functions/lib/Facebook/Entities/AccessToken.php');
require_once ('/includes/php_functions/lib/Facebook/HttpClients/FacebookCurl.php');
require_once ('/includes/php_functions/lib/Facebook/HttpClients/FacebookHttpable.php');
require_once ('/includes/php_functions/lib/Facebook/HttpClients/FacebookCurlHttpClient.php');
/* USE NAMESPACES */
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;
use Facebook\FacebookHttpable;
use Facebook\FacebookCurlHttpClient;
use Facebook\FacebookCurl;
/*PROCESS*/
if (!isset($_COOKIE['logged_in'])) {
$loginUrl = "";
$app_id = 'APP_ID';
$app_secret = 'APP_SECRET';
$redirect_url = 'http://localhost:9080/';
try {
FacebookSession::setDefaultApplication($app_id, $app_secret);
$helper = new FacebookRedirectLoginHelper($redirect_url);
$sess = $helper -> getSessionFromRedirect();
//this if block never happens because session will not set after redirect :/
if (isset($sess)) {
//create request object,execute and capture response
$request = new FacebookRequest($sess, 'GET', '/me');
// from response get graph object
$response = $request -> execute();
$graph = $response -> getGraphObject(GraphUser::classname());
// use graph object methods to get user details
$name = $graph -> getName();
$social_user_id = $graph -> getId();
$profile_photo_url = 'https://graph.facebook.com/' . $social_user_id . '/picture?width=300';
$email = $graph -> getProperty('email');
$token = $sess -> getToken();
//set cookies
setcookie("logged_in", true, time() + 86400, "/");
header("Location: http://localhost:9080/");
exit();
} else {
$loginUrl = $helper -> getLoginUrl(array('email'));
}
} catch (Exception $e) {
}
}