Jump to content
ruicosta.web

Facebook SDK 4

Recommended Posts

ruicosta.web

Boa tarde,

Estou a aventurar-me com a api do facebook.

Para já estou a testar apenas o login. Está tudo a funcionar em pleno, mas estranhamente, sempre que faço F5 para atualizar a página, automaticamente faz logout e obriga-me a clicar no butao de login. Algum ideia?

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
session_start();
require_once( 'lib/facebook/FacebookSession.php' );
require_once( 'lib/facebook/FacebookRedirectLoginHelper.php' );
require_once( 'lib/facebook/FacebookRequest.php' );
require_once( 'lib/facebook/FacebookResponse.php' );
require_once( 'lib/facebook/FacebookSDKException.php' );
require_once( 'lib/facebook/FacebookRequestException.php' );
require_once( 'lib/facebook/FacebookAuthorizationException.php' );
require_once( 'lib/facebook/GraphObject.php' );
require_once( 'lib/facebook/Entities/AccessToken.php' );
require_once( 'lib/facebook/Entities/SignedRequest.php' );
require_once( 'lib/facebook/HttpClients/FacebookHttpable.php' );
require_once( 'lib/facebook/HttpClients/FacebookCurl.php' );
require_once( 'lib/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\Entities\AccessToken;
use Facebook\Entities\SignedRequest;
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('xxxxxxx','xxxxx');
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://localhost/fb' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
if ( isset( $session ) ) {
 // Dados do Utilizador
 $request = new FacebookRequest(
	   $session,
	   'GET',
	   '/me'
 );

$response = $request->execute();

$graphObject = $response->getGraphObject();

  echo '<a href="' . $helper->getLogoutUrl($session, 'http://localhost/fb/index.php') . '"><button type="button" class="btn btn-lg btn-success"><i class="fa fa-facebook-official fa-5x"></i> </button></a>';
} else {
  echo '<a href="' . $helper->getLoginUrl() . '"><button type="button" class="btn btn-lg btn-danger"><i class="fa fa-facebook-official fa-5x"></i></button></a>';
}
?>
 

Share this post


Link to post
Share on other sites
ruicosta.web

Ja tentaste adicionar uma session ?

Não. variavel e valor? Já andei às voltas na documentação e não vi nada. Claro que não devo ter visto por não saber muito bem o que procuro :)

Share this post


Link to post
Share on other sites
peterpah

tens que guardar a session que inicias-te numa variável e verificar depois com um if se esta iniciada ou nao

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.