Hi i am trying to make a request with a app session and its saying this : "(#200) Must have a valid access_token to access this endpoint". I can make requests only with a app session right?
Below its the code its giving me this error.
<?php
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookPermissionException.php');
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php');
require_once( 'Facebook/HttpClients/FacebookHttpable.php');
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.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;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( $facebook_app_id, $facebook_app_secret);
$session = FacebookSession::newAppSession();
// To validate the session:
try {
$session->validate();
} catch (FacebookRequestException $ex) {
// Session not valid, Graph API returned an exception with the reason.
echo $ex->getMessage();
} catch (\Exception $ex) {
// Graph API returned info, but it may mismatch the current app or have expired.
echo $ex->getMessage();
}
if(isset($session)){
try {
$response = (new FacebookRequest($session, 'GET', '/search?q=coimbra&type=event&limit=5000'))->execute();
$object = $response->getGraphObject();
echo "done.";
} catch (FacebookRequestException $ex) {
echo $ex->getMessage();
} catch (\Exception $ex) {
echo $ex->getMessage();
}
}
?>
The error means you must have a valid USER access_token to preform searches. This is because the search results will filter out events (or other content) the user isn't allowed to see, e.g. private events, or closed event. But it also means you find event the user does have permission to see.
In summary, you need to log the user in to get a valid user access_token for the search to work.
Related
For example, I want to retrieve my name using Facebook PHP SDK 4.
For that I use this code
<?php
require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/Entities/SignedRequest.php' );
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookOtherException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.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;
FacebookSession::setDefaultApplication('app_id', 'app_secret');
// If you already have a valid access token:
$session = new FacebookSession('access token');
// To validate the session:
try {
$session->validate();
} catch (FacebookRequestException $ex) {
// Session not valid, Graph API returned an exception with the reason.
echo $ex->getMessage();
} catch (\Exception $ex) {
// Graph API returned info, but it may mismatch the current app or have expired.
echo $ex->getMessage();
}
try {
$response = (new FacebookRequest($session, 'GET', '/me'))->execute();
$object = $response->getGraphObject();
echo $object->getProperty('name');
} catch (FacebookRequestException $ex) {
echo $ex->getMessage();
} catch (\Exception $ex) {
echo $ex->getMessage();
}
?>
I get my access token from this link :
https://graph.facebook.com/oauth/access_token?client_id={app-id} &client_secret={app-secret}&grant_type=client_credentials
When I execute the code I get this error :
An active access token must be used to query information about the
current user.
What i'm doing wrong ? Thanks
I am using facebook api for getting feeds from facebook page.
Here is the code I am using:
session_start();
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once ('Facebook/GraphObject.php' );
require_once ('Facebook/GraphSessionInfo.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once('Facebook/HttpClients/FacebookHttpable.php');
require_once( 'Facebook/HttpClients/FacebookCurl.php');
require_once ( 'Facebook/HttpClients/FacebookCurlHttpClient.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;
FacebookSession::setDefaultApplication('app_id', 'app_secret');
$session = new FacebookSession('access_token');
$session = FacebookSession::newAppSession();
// To validate the session:
try {
$session->validate();
} catch (FacebookRequestException $ex) {
// Session not valid, Graph API returned an exception with the reason.
echo $ex->getMessage();
}
$request = new FacebookRequest(
$session,
'GET',
'/{page-id}/feed'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
I have added app_id, app_secret,page-id and access_token in the code. But I am getting the error
Fatal error: Uncaught exception 'Facebook\FacebookAuthorizationException' with message '(#803) Some of the aliases you requested do not exist: {page-id}
But if I open https://graph.facebook.com/page-id?access_token=my_access_token, I get the required data.
I am doing all the stuff on localhost. Can some one help me finding the error?
I'm new to the facebook login SDK for PHP and i'm trying to create a "log in with facebook" function on my website.
I'm using this code which works great:
session_start();
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.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\FacebookCurl;
use Facebook\FacebookCurlHttpClient;
use Facebook\FacebookHttpable;
FacebookSession::setDefaultApplication('app-id', 'app-secret');
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'app-redirect-link' );
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();
// print data
echo print_r( $graphObject, 1 );
} else {
// show login url
echo 'Login';
}
If I press "Login" I get my details and everything I need, but as soon as i'm refreshing the page - the "Login" link appears again, as if the session does not exist anymore.
Any help would be appreciated.
Defining a new session and using it helped.
<?php
// include required files form Facebook SDK
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookCanvasLoginHelper.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookPermissionException.php' );
require_once( 'Facebook/FacebookOtherException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
require_once( 'Facebook/GraphUser.php' );
require_once( 'Facebook/GraphLocation.php' );
include ("fns.php");
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookCanvasLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
use Facebook\GraphUser;
use Facebook\GraphLocation;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( 'XXXXXXXXXXx','yyyyyyyyyyyyyyyy' );
$helper = new FacebookCanvasLoginHelper();
try {
$session = $helper->getSession();
} catch (Exception $ex) {
// echo $ex->getMessage();
}
if($session)
{
$request = new FacebookRequest( $session, 'GET', '/me' );
try{
$response = $request->execute();
$graphObject = $response->getGraphObject();
//echo print_r( $graphObject, 1 );
$fid = $graphObject->getProperty('id'); echo $fid;
$femail = $graphObject->getProperty('email'); echo $femail;
$ffirst_name = $graphObject->getProperty('first_name'); echo $ffirst_name;
} catch (Exception $ex) {
// echo $ex->getMessage();
}
}
?>
First off, I am not an expert PHP coder, so I do not understand a lot of things, but I do know how to use a working example. I made a Canvas App that was working fine. Then I installed ssl and it no longer works. The code itself is a mess from my efforts to fix the problem.
Can I just get a working example that echos user public data using FacebookCanvasLoginHelper with the latest 4.06 PHP SDK? I can take it up from there if I can see that.
Since V4.0, getsession() no longer seems to work. In V4.06 its supposed to have been fixed, but I still cant get it to work.
Thanks in advance
Edit
I have added the relevant code. After debugging, i find that it appears $session is not valid or something because code execution never gets past "$request = new FacebookRequest( $session, 'GET', '/me' );". I cant seem to get anything echoed out after that. please any help is much appreciated.
If it worked previously and you ONLY changed to SSL then the only thing that would cause the error and lack of session is a redirect mismatch.
Your old app redirect would be http and the new app redirect will be https
Change the Facebook app to reflect this change in the app redirect set up.
php 5.4 is required.
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /var/www/Facebook/FacebookResponse.php on line 137
I can't get user id with facebook php sdk :|. Anyone can help me ? this is the code.
I spend like 4 hours to get some results but nothing. with sdk 3.2.3 works fine but with 4 i can't get it to work.
<?php
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( '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;
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('','');
$helper = new FacebookCanvasLoginHelper();
try {
$session = $helper->getSession();
} catch (FacebookRequestException $ex) {
// When Facebook returns an error
} catch (\Exception $ex) {
// When validation fails or other local issues
}
if ($session) {
try {
$user_profile = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
echo "Name: " . $user_profile->getName();
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
// Logged in.
}
Something like this should work. You have to go to your facebook developer panel and generate an access token for this app by clicking "You need to grant permissions to your app to get an access token."
Then your code should be something like this:
$session = new FacebookSession('your-token');
try {
$me = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
echo $me->getName();
} catch (FacebookRequestException $e) {
// The Graph API returned an error
} catch (\Exception $e) {
// Some other error occurred
}
I think your app is just not well initiated with your app ID and Secret.
But you can no more get the real user id with the new graph api and php sdk.
Read their changelog for details