Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Facebook Graph API v5.4.2 #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[submodule "include/flight"]
path = include/flight
url = https://github.com/mikecao/flight/
[submodule "include/facebook-php-sdk-v4"]
path = include/facebook-php-sdk-v4
url = https://github.com/facebook/facebook-php-sdk-v4.git
[submodule "static/fontawesome"]
path = static/fontawesome
url = https://github.com/FortAwesome/Font-Awesome.git
[submodule "include/php-graph-sdk"]
path = include/php-graph-sdk
url = https://github.com/facebook/php-graph-sdk
83 changes: 38 additions & 45 deletions handlers/fb_handlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,20 @@
// and Facebook

// TODO: this only works if the script is installed in root
define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__ . '/../include/facebook-php-sdk-v4/src/Facebook/');
require_once(__DIR__ . '/../include/facebook-php-sdk-v4/autoload.php');
define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__ . '/../include/php-graph-sdk/src/Facebook');
require_once(__DIR__ . '/../include/php-graph-sdk/src/Facebook/autoload.php');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The autoloader was removed on facebookarchive/php-graph-sdk@877e977. Maybe set branch = 5.4 on .gitmodules fix this.

Copy link
Contributor Author

@teldosas teldosas Nov 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @phelipebf
As shown here the submodule php-graph-sdk references facebookarchive/php-graph-sdk@2839246 which is identical to branch 5.4.

This helped me to better understand git submodules.

Should I set branch =5.4 on .gitmodules anyway?

Copy link

@phelipebf phelipebf Nov 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessary @teldosas. Forgive my lack of attention. Your commit 0e8707c deal with this.


require_once(__DIR__ . '/../tokens.php');

use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;

FacebookSession::setDefaultApplication(APP_ID,
APP_SECRET);
Flight::set('fb', new Facebook([
'app_id' => APP_ID,
'app_secret' => APP_SECRET,
'default_graph_version' => 'v2.8',
]));


Flight::set('retry_url', MY_URL .'login');
Expand Down Expand Up @@ -67,25 +69,20 @@ function render_boilerplate() {
}


function check_permissions($session) {

$request = new FacebookRequest(
$session,
'GET',
'/me/permissions'
);

function check_permissions($accessToken) {

$fb = Flight::get('fb');

try {
$response = $request->execute();
$graphObject = $response->getGraphObject()->asArray();
// http://stackoverflow.com/q/23527919
foreach ($graphObject as $key => $permissionObject) {
//print_r($permission);
if ($permissionObject->permission == 'publish_actions') {
return $permissionObject->status == 'granted';
$response = $fb->get('/me/permissions', $accessToken);
$graphEdge = $response->getGraphEdge();

foreach($graphEdge as $graphNode) {
if ($graphNode->getField('permission') == 'publish_actions') {
return $graphNode->getField('status') == 'granted';
}
}
} catch (FacebookRequestException $ex) {
} catch (FacebookResponseException $ex) {
Flight::error($ex);
} catch (\Exception $ex) {
Flight::error($ex);
Expand All @@ -107,12 +104,13 @@ function handle_root() {
// if the user does not grant publish_actions,
// we go here and ask again
function handle_rerequest_permission() {
$fb = Flight::get('fb');
render_boilerplate();
// Simplification: always assume we are not logged in!
$helper = new FacebookRedirectLoginHelper(MY_URL . 'fb_callback/');
$helper = $fb->getRedirectLoginHelper();
// We do want to publish to the user's wall!
$scope = array('publish_actions');
$fb_login_url = $helper->getReRequestUrl($scope);
$fb_login_url = $helper->getReRequestUrl(MY_URL . 'fb_callback/', $scope);
Flight::render('rerequest_permission', array(
'fburl' => $fb_login_url,
));
Expand All @@ -121,20 +119,21 @@ function handle_rerequest_permission() {
// In the FB callback, we show a form to the user
// or an error message if something went wrong.
function handle_fb_callback() {
$fb = Flight::get('fb');
render_boilerplate();
$helper = new FacebookRedirectLoginHelper(MY_URL . 'fb_callback/');
$helper = $fb->getRedirectLoginHelper();
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
$accessToken = $helper->getAccessToken();
} catch(FacebookSDKException $ex) {
// When Facebook returns an error
Flight::error($ex);
} catch(\Exception $ex) {
// When validation fails or other local issues
Flight:error($ex);
}
if ($session) {
$_SESSION['FBTOKEN'] = $session->getToken();
if (check_permissions($session)) {
if (isset($accessToken)) {
$_SESSION['FBTOKEN'] = (string) $accessToken;
if (check_permissions($accessToken)) {
$_SESSION['FB_CHECKIN_NONCE'] = make_nonce();
Flight::render('fb_callback', array(
'post_action' => MY_URL .'checkin',
Expand All @@ -160,6 +159,7 @@ function handle_fb_callback() {
}

function handle_checkin() {
$fb = Flight::get('fb');
render_boilerplate();
// This happens if we unset the nonce below.
// Or if the nonce was never set, in which case the user
Expand Down Expand Up @@ -195,29 +195,22 @@ function handle_checkin() {
if (empty($token)) {
Flight::error(new Exception('No FB token in session!'));
}
$session = new FacebookSession($token);
$message = Flight::request()->query->message;

$config = array('place' => PAGE_ID);
if (! empty($message)) {
$config['message'] = $message;
}
$request = new FacebookRequest(
$session,
'POST',
'/me/feed',
$config
);
// Some exceptions can be caught and handled sanely,
// e.g. Duplicate status message (506)
try {
$response = $request->execute()->getGraphObject();
} catch (FacebookRequestException $ex) {
$response = $fb->post('/me/feed', $config, $token);
} catch (FacebookResponseException $ex) {
Flight::error($ex);
} catch (\Exception $ex) {
Flight::error($ex);
}
$postid = $response->asArray()['id'];
$postid = $response->getGraphNode()->getField('id');
$posturl = 'https://www.facebook.com/' . $postid;
Flight::render('checkin',
array(
Expand All @@ -227,17 +220,17 @@ function handle_checkin() {
}

function fblogin() {

$fb = Flight::get('fb');
// Simplification: always assume we are not logged in!
$helper = new FacebookRedirectLoginHelper(MY_URL . 'fb_callback/');
$helper = $fb->getRedirectLoginHelper();
// We do want to publish to the user's wall!
// Note: Facebook docs state that login and write permission request
// should be two separate requests.
// The code is already set up to handle this separately, but I believe
// the combined flow provides better UX.
// https://developers.facebook.com/docs/facebook-login/permissions/v2.2
$scope = array('publish_actions');
$fb_login_url = $helper->getLoginUrl($scope);
$fb_login_url = $helper->getLoginUrl(MY_URL . 'fb_callback/', $scope);
$code_login_url = MY_URL . 'access_code/';
Flight::render('login', array(
'fburl' => $fb_login_url,
Expand Down
1 change: 0 additions & 1 deletion include/facebook-php-sdk-v4
Submodule facebook-php-sdk-v4 deleted from c82611
1 change: 1 addition & 0 deletions include/php-graph-sdk
Submodule php-graph-sdk added at 283924