I am working on a website in which there are more than three Facebook Page Like Button. Once user click on the button. First, I need to authenticate the user and then like the respective page. However User authentication is working fine now I need to Like a page. I am following the Facebook Like Documentation. But I am getting confused with {object-id} metioned in the documentation. What is this object-id and How do I get this ?
Like Page Code :
session_start();
require_once 'autoload.php';
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;
FacebookSession::setDefaultApplication( 'App_ID','App_Secret' );
$helper = new FacebookRedirectLoginHelper('http://localhost/fb/like.php' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// Facebook returns an error
} catch( Exception $ex ) {
// validation fails or other local issues
}
if ( isset( $session ) ) {
// Get Basic Userinfo
$getUserInfoObj = new FacebookRequest( $session, 'GET', '/me' );
$getUserInfoProcess = $getUserInfoObj->execute();
$getUserInforesult = $getUserInfoProcess->getGraphObject();
$id = $getUserInforesult->getProperty('id');
// Once Use Logged In Like the respective page
// Like page code
echo '<pre>';print_r($getUserInforesult);exit;
//header("Location: index.php");
} else {
$loginUrl = $helper->getLoginUrl();
header("Location: ".$loginUrl);
}
Object Id is your page, comment, post even photo id that want to make liked.
For example stackoverflow facebook page link is,
https://www.facebook.com/pages/Stack-Overflow/105665906133609?fref=ts
so the object_id is 105665906133609
Related
I did everything perfect but when i click on the link of the login helper it redirects me to the facebook page and gives this error:
Can't load URL: The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and sub-domains of your app to the App Domains field in your app settings.
Any suggestions?
my facebook developer app settings
<?php
session_start();
// added in v4.0.0
require_once 'autoload.php';
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;
// start session
// init app with app id and secret
FacebookSession::setDefaultApplication( '367245673760849','my secret goes here' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('http://saturnproduction.com/test.php' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbuname = $graphObject->getProperty('username'); // To Get Facebook Username
$fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
$femail = $graphObject->getProperty('email'); // To Get Facebook email ID
/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['USERNAME'] = $fbuname;
$_SESSION['FULLNAME'] = $fbfullname;
$_SESSION['EMAIL'] = $femail;
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
} else {
// show login url
echo 'Login';
}
?>
You have to input all the URLs in Valid OAuth Redirect URIs.
API Documentation : https://developers.facebook.com/docs/facebook-login/security/#surfacearea
Go to App Dashboard, then under Products /Facebook Login /Setting
Link : https://developers.facebook.com/apps/App_ID/fb-login/settings/
Replace it with your App_ID = 367245673760849
According to the code sniped mentioned above URL should be http://saturnproduction.com/test.php
Refer Screenshot if any doubts:
I am working on a website in php where i have integrated a Facebook Connect API which enabled the Facebook login button.
My code to get user details looks like this:
<?php
session_start();
// added in v4.0.0
require_once 'autoload.php';
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\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
// init app with app id and secret
$appID = 'app id';
$appSecret = 'app secret code';
FacebookSession::setDefaultApplication( $appID, $appSecret);
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('http://localhost/test/facebook_new/fbconfig.php' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$profile = $response->getGraphObject('Facebook\GraphUser');
var_dump($profile);
} else {
$loginUrl = $helper->getLoginUrl();
header("Location: ".$loginUrl);
}
?>
After eecuting this code, var_dump($profile) displays only the User Facebook ID and his Full Name as displayed on the picture below:
What I want to get is the email. The email can be accessed through 'Facebook\GraphUser' but I really dont know what is wrong with my code.
Kindly help me solve this problem
$request = new FacebookRequest($session, 'GET', '/me?fields=name,email');
It is called "Declarative Fields", you have to specify the fields you want to get returned since v2.4 of the Graph API.
Also, make sure the user is authorized with the email permission.
Facebook users in their settings can set their email as private or public. This might be the reason it is not showing up for you.
i tried to use the latest sdk of php to develop app with my website. but i always not get the correct session. then, i check the sdk file and found the isValidRedirect() return false.
i tried to change the app setting, but not working for me.
so, guys, please check the attachment and where is wrong?
there is my code below:
// localhost/facebook/demo.php
<?php
#session_start();
// added in v4.0.0
// Skip these two lines if you're using Composer
require __DIR__ . '/autoload.php';
//require 'functions.php';
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;
// init app with app id and secret
FacebookSession::setDefaultApplication( 'app_id','app_secrect' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('http://demo.localhost/facebook/demo.php/' );
$loginUrl = $helper->getLoginUrl();
echo '<hr />';
echo $loginUrl;
echo '<hr />';
try {
$session = $helper->getSessionFromRedirect();
// $request = new FacebookRequest( $session, 'GET', '/me' );
// print_r($request);
} catch( FacebookRequestException $ex ) {
echo 22;
// When Facebook returns an error
} catch( Exception $ex ) {
echo 33;
// When validation fails or other local issues
}
?>
Login with Facebook</div>
thanks.
php file url:
demo.localhost/facebook/demo.php
Secure Canvas URL:
demo.localhost/facebook/
Website:
site url:
demo.localhost/facebook/
Mobile Site Url:
demo.localhost/facebook/
Deauthorize Callback URL:
https://demo.localhost/facebook/
I had the same issue today, and I noticed that the state is always different between the one in the session and the returned state.
After a few googling, someone mentioned in another post
(PHP facebook SDK 4.0 login error) that the function getLoginUrl() changes state variable value. As suggested in that post and the comment, you need to move your $helper->getLoginUrl() after the $helper->getSessionFromRedirect(). It works for me.
I hope this helps.
I'm updating my apps to work with the new PHP SDK, but I'm having this issue:
I want to do the Facebook stuff in a separate php file to keep things clean.
For example, this is my fb.php page:
<?php
require_once('sdk/autoload.php');
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\Entities\AccessToken;
use Facebook\Entities\SignedRequest;
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication(...,...);
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
}
// see if we have a session
if ( isset( $session ) ) {
// save the session
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
//logged in
} else {
//user is not logged in
?>
<script>window.location="...link to login page...";</script>
<?php
exit;
}
A page which uses the Facebook session, could look like this (testing.php):
<?php
require_once('fb.php');
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
$graphObject = $response->getGraphObject()->asArray();
echo "Welcome, ".$graphObject['first_name'];
This returns this error:
Fatal error: Class 'FacebookRequest' not found
The code works fine when I put use Facebook\FacebookRequest; in testing.php , but I don't want to do that, since that was the point of putting all the Facebook session stuff in a separate fb.php file...
What is the reason for this, and is there a solution how to keep all my Facebook stuff in a separate file?
Thanks!
File in which you are including fb.php is in different scope so can't use that imports.
You have to specify them again.
You can read more about that in PHP manual.
I am a PHP developer and what I want is firstly user come to my website and login using Facebook login then I want to redirect user to specific page and get access token and then call to Graph API to access user's data. I have divided it into 2 steps
1) Make user to login using Facebook, It's done and code is given below:
include_once('facebook-php-sdk-v4-master/src/Facebook/FacebookSession.php');
include_once('facebook-php-sdk-v4-master/src/Facebook/FacebookRedirectLoginHelper.php');
require_once( 'facebook-php-sdk-v4-master/src/Facebook/FacebookRequest.php' );
require_once( 'facebook-php-sdk-v4-master/src/Facebook/FacebookResponse.php' );
require_once( 'facebook-php-sdk-v4-master/src/Facebook/FacebookSDKException.php' );
require_once( 'facebook-php-sdk-v4-master/src/Facebook/FacebookRequestException.php' );
require_once( 'facebook-php-sdk-v4-master/src/Facebook/FacebookAuthorizationException.php' );
require_once( 'facebook-php-sdk-v4-master/src/Facebook/GraphObject.php' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
session_start();
FacebookSession::setDefaultApplication('APP id', 'APP key');
$helper = new FacebookRedirectLoginHelper('http://redirect-url.com/');
$loginUrl = $helper->getLoginUrl();
echo "FB login here";
2) Get token detail on redirect URL, which is incomplete
Can anyone please help me how can I get this detail ?????
See the following tutorial on how to handle login and sessions correctly.
To get the token after redirect, you need to so the following:
$session = $helper->getSessionFromRedirect();
Then, check if the session is valid and redirect as needed:
if ( isset( $session ) ) {
// user is logged in, display logout link
echo 'Logout';
} else {
// show login url
echo 'Login';
}
Even I was going getting the same error.
Found another method to get session information on another page.
On first page
FacebookSession::setDefaultApplication($app_id,$app_secret,$cookie);
// login helper with redirect_uri
$helper = new FacebookCanvasLoginHelper();
try {
$session = $helper->getSession();
$_SESSION['token'] = $session->getToken();
}
On the second page it should be
$token = $_SESSION["token"];
$session = new FacebookSession($token);