Php with Facebook SDK don't load page - php

I use this code to take information about people friends with Facebook
:
require 'facebook-php-sdk-master/src/facebook.php';
$app_id = 'xxxxxxx';
$app_secret = 'xxxxxxxx';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('me/friends');
print_r($user_profile);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
} else {
$params = array(
'scope' => 'user_birthday',
);
$loginUrl = $facebook->getLoginUrl($params);
echo("<script> top.location.href='" . $loginUrl . "'</script>");
}
?>
But now, a lot of time, when i try to load this , i see that in the url the param "code" start to change a lot of time and the page don't load. What kind of problem is that?

$user is false in your code for some reason (always). May be the facebook user didn't accept your app. $facebook->getLoginUrl($params); redirects to the request uri, with $user false the request uri redirects to $loginUrl. This cause an endless loop.

Related

Facebook re-authentication

I'm using PHP SDK to authentication and requesting permissions.
$facebook = new Facebook(array(
'appId' => '7xxx349xx',
'secret' => 'x0b360034c9exxxx4f',
'cookie' => true
));
$params = array(
'scope' => 'publish_actions, read_friendlists',
'redirect_uri' => 'example.com',
);
$user = $facebook->getUser();
$usertoken = $facebook->getAccessToken();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
header("Location: xxx.php");
}
if(!$user) {
//$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook->getLoginUrl($params);
}
It works well but if user delete app in privacy settings and try to login again I have errors:
CSRF state token does not match one provided.
OR
OAuthException: Error validating access token: The user has not authorized application [APPID]
How to fix it? I need additional code?
Thanks for help

Php Facebook login script ends up in a redirect loop and does not login

This code always runs the last else part and redirects to the login URL provided by facebook api. If i print the $user variable, it just shows 0 everytime.
$facebook = new Facebook(array(
'appId' => 'app id',
'secret' => 'app secret',
));
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
$_SESSION['logouturl'] = $logoutUrl;
$_SESSION['user'] = $user_profile['name'];
$_SESSION['id'] = $user_profile['id'];
$_SESSION['type'] = 'facebook';
header('Location: index.php');
} else
{
$loginUrl = $facebook->getLoginUrl();
header('Location: '.$loginUrl);
}
?>
$facebook = new Facebook(array(
'appId' => 'xx',
'secret' => 'xx',
'scope' => 'read_stream, email'
));
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$params = array(
'scope' => 'read_stream, email'
);
$loginUrl = $facebook->getLoginUrl($params);
make sure your app setting on Facebook is good, rest is looking fine and if you have minor problem in you app setting on Facebook developer console it wont work specially domain name and domain url settings

Fceabook PHP SDK valid access token

I have search over and over again and I can't seem to find an answer. What am I doing wrong?
My error is FacebookApiException [ 0 ]: An active access token must be used to query information about the current user.
EDIT Just to add. This worked once or twice, but never in succession.
require_once('media/fb/facebook.php');
$app_id = '123456';
$app_secret = '123456';
$my_url = 'mysiteurl';
$config = array(
'appId' => $app_id,
'secret' => $app_secret,
'fileUload' => 'false');
$facebook = new Facebook($config);
$user = $facebook->getUser();
if($user) {
try {
$user_profile = $facebook->api('/me','GET');
} catch(FacebookApiException $e) {
error_log($e->getType());
error_log($e->getMessage());
}
} else {
$login_url = $facebook->getLoginUrl(array('redirect_uri' => $my_url));
header("Location:" . $login_url);
$user_profile = $facebook->api('/me','GET');
}
Your help is greatly appreciated.
Does your error come from this line: $user_profile = $facebook->api('/me','GET'); at the bottom?
The header function will re-direct the user to Facebook login page which will cause the browser to refresh. You won't get the user's access token immediately after that. You need to wait until the user is redirected back to your website to get the access token. You need to set $my_url to point to the same page of the codes you wrote here.

facebook redirect

I have a problem when using facebook authentication.
<?php
$user = null;
try{
include_once "fb/facebook.php";
}
catch(Exception $o){
error_log($o);
}
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true
));
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'publish_stream,email',
//'redirect_uri' => 'https://www.facebook.com/pages/MediaEngine/304221192963546?id=304221192963546&sk=app_143090482526977'
));
$user = $facebook->getUser();
if($user){
try{
$user_profile = $facebook->api('/me');
$permissions = $facebook->api('/me/permissions');
}catch(FacebookException $e){
}
}else{
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
}
?>
When the user accepted the app, it will redirect user to tab page(http://apps4you.hu), user wont stay on facebook, but only 1st time will do that if the user close and come back to app on facebook(already authenticated) it works fine in iframe.
If I use the redirect_uri => my_app , it will redirect and redirect and redirect but never will work the app, the browser just loading and loanding but nothing happends.
I spent days to fix this problem but I could not find any solution.
Any idea whats wrong?
Change $loginUrl as below and try:
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); // CSRF protection
$loginUrl _url = "https://www.facebook.com/dialog/oauth?client_id="
. APP_ID . "&redirect_uri=" . urlencode($next) . "&state="
. $_SESSION['state'] . "&scope=".$permissions;

Direct access after authentication insteed iframe access

When the user authorize my app, he is redirected to my site and not to facebook with my app in the iframe. Is there a way to stay in facebook ?
My code :
require './src/facebook.php';
$config = array();
$config['appId'] = 'XXXXX';
$config['secret'] = 'XXXXX';
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
echo "blah";
} else {
$params = array(
'scope' => 'read_stream, friends_likes',
'redirect_uri' => 'https://xxxxx'
);
$loginUrl = $facebook->getLoginUrl($params);
echo("<script> top.location.href='" . $loginUrl . "'</script>");
}
?>
Try putting your app's url (http://apps.facebook.com/your_app_namespace) in the redirect_uri param. This should fix the issue you are facing.

Categories