I wrote 4 pages worth of text and it disappeared because it claimed I wasn't logged in. Granted this is my first post... I cannot find anyone on the web who can help me with figuring out this rudimentary Facebook SDK.... I have read for hours and hours... and have coded and uploaded to my server and cannot get anything to work.... I can get all the data that I need from the Graph Explorer, but I cannot seem to code it.
I am new (somewhat) to PHP, but have a strong pythong background. PLUS, I can modify Wordpress Themes with ease... I think Wordpress made me feel like I knew more about PHP than I really did....!!!:mad:
Bottom Line: I am having an inordinate amount of problems with using the Facebook API....specifically, the PHP SDK.
The problem is I lose variables. I understand include and require, but there is a instantiation of a facebook class that redirects the user after he/she logs in back to a url that you specify, but there is no session data when it returns.
All I want to do is check for authorization... if I have it, I want to check for Extended Privileges, and get them if necessary. If I do not have a valid session, I want to take the steps tp get one... I have followed all of examples around the web but upon redirect, I don't have a session variable.... and I get errors like
Fatal error: Call to a member function getAccessToken() on a non-object in /var/www/html/Facebook/NewVersion/after-login.php on line 18
I am going to paste my code as it is not very long...
My first file is just the index.php
<?php
session_start();
require 'functions.php';
require 'autoload.php'; //grabs all the SDK stuff including 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\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
?>
?>
<!doctype html>
<html>
<head>
<title>Login To Facebook</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
<?php
// start session
// init app with app id and secret
FacebookSession::setDefaultApplication( '968678193151708','f04a8d3ebe82949efbc1215840055df4' );
// login helper with redirect_uri
$redirect_url = "http://dev.resultscloud.net/Facebook/NewVersion/after-login.php";
$helper = new FacebookRedirectLoginHelper($redirect_url);
//should put this in a function
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
$session = null;
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
$session = null;
}
if ($session ) { // User is already logged or has logged in per this script
// User logged in, get the AccessToken entity.
$accessToken = $session->getAccessToken();
// Exchange the short-lived token for a long-lived token.
$longLivedAccessToken = $accessToken->extend();
// Now store the long-lived token in the database
// . . . $db->store($longLivedAccessToken);
// Make calls to Graph with the long-lived token.
} else { // before any login by user
include 'fbconfig.php'; }
?>
</body>
</html>
Then the next file if the fbconfig.pho...which is just a link to go to facebook and get authorization.
<?php
//Scope to get Extended_Permissions
$scope = array('manage_pages, publish_actions');
// This will be the login URL if needed
$loginUrl = $helper->getLoginUrl($scope);
?>
<div class="container bg-warning">
<h1>Login with Facebook</h1>
<h3 class="bg-important">Not Connected</h3>
<?php echo '<div class="span7 text-center"><a href="' . $loginUrl . '" class="btn btn-primary .btn-lg" role="button">Login To Facebook</a></div>'; ?>
</div>
Lastly is the redirect URL which is where I want to read my data...(I need page level data)... but I cannot seem to keep the session.
This file is called after-login.php since it is the redirect back from facebook:
<?php
$helper = new FacebookRedirectLoginHelper($redirect_url);
// see if we have a session
if ( $session ) {
// graph api request for user data
try {
$request = new FacebookRequest($session, 'GET', '/me');
$response = $request->execute();
$graphObject = $response->getGraphObject();
} catch (FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
// Extend Access Token
$accessToken = $session->getAccessToken();
try {
// Exchange the short-lived token for a long-lived token.
$longLivedAccessToken = $accessToken->extend();
} catch(FacebookSDKException $e) {
echo 'Error extending short-lived access token: ' . $e->getMessage();
exit;
}
pp($graphObject);
pp($response);
// Below (for reference) we are getting the data items using getProperty() since it is the graphObject class and not using a method of the graphUser subclass (e.g., getName(), getFirstName(), getBirthday(), etc.)
// $fbid = $graphObject->getProperty('id'); // To Get Facebook ID
// $fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
// $femail = $graphObject->getProperty('email'); // To Get Facebook email ID
/* ---- Session Variables -----*/
// $_SESSION['FBID'] = $fbid;
// $_SESSION['FULLNAME'] = $fbfullname;
// $_SESSION['EMAIL'] = $femail;
// // Get an Extended User Access Token
// $longLivedSession = $facebookSession->getLongLivedSession();
// echo $longLivedSession->getToken();
// // Get Page Info since we should have an extended permissions
// $request = new FacebookRequest($session, 'GET', '/me/accounts?fields=name,access_token,perms');
// Chaining methods here:
// $pageList = $request->execute()->getGraphObject()->asArray();
// foreach ($pageList as $page) {
// $pageAccessToken = $page['access_token'];
// }
// $pageToken = $result['access_token'];
// $facebookSession = new FacebookSession($pageToken);
// header("Location: index.php")
//
// echo "<pre>";
// print_r($graphObject);
// $data = $graphObject->getProperty('data');
// print_r($data);
// echo "a".$data->getProperty('access_token')."<br>";
// if (is_object($data)) echo "I am an object";
// echo "</pre>";
// // $fbid = $graphObject->getProperty('id'); // To Get Facebook ID
// // $fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
// //$femail = $graphObject->getProperty('email'); // To Get Facebook email ID
// /* ---- Session Variables -----*/
// //$_SESSION['FBID'] = $fbid;
// //$_SESSION['FULLNAME'] = $fbfullname;
// //$_SESSION['EMAIL'] = $femail;
// /* ---- header location after session ----*/
// //header("Location: index.php");
// } else {
// $loginUrl = $helper->getLoginUrl($scope);
// header("Location: ".$loginUrl);
// }
?>
I am sorry if I sound exasperated, but I have been up for 72 hours trying to get this to work... Nobody knows this API or I cannot find a forum which could help me... I stumbled upon this one and it looked like it was professional and there were some knowledgeable coders here...
Any help that you can provide is greatly appreciated.