Facebook redirect loop when running locally - facebook

I'm working on a local Facebook project. But when I go to localhost, he starts redirecting to localhost/?code=xxxx everytime (xxxx is very long random characters).
My code is:
require_once('sdk/src/facebook.php');
$facebook = new Facebook(array(
'appId' => 'MY_APP_ID',
'secret' => 'MY_APP_SECRET',
'sharedSession' => true,
'trustForwarded' => true,
'cookie' => true
));
$user_id = $facebook->getUser();
if ($user_id) {
try {
// Fetch the viewer's basic information
$basic = $facebook->api('/me','GET');
} catch (FacebookApiException $e) {
if (!$facebook->getUser()) {
//header('Location: '. AppInfo::getUrl($_SERVER['REQUEST_URI']));
$user_id = null;
}
}
}
if(!$user_id) {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email,publish_actions'
));
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit();
}
...
My app settings:
Thanks in advance.

The likely issue is that your app is returning 0 for $user_id even after the user has logged in, causing the JavaScript redirect to happen again, causing it to loop forever.
The case of the issue may be one of many things, but make sure the code GET variable isn't being stripped out of the URL by a .htaccess file or other means. Secondly, take a look at this modification to the base_facebook.php file to change the way the code is checked.

Related

facebook app wont ask for extended permissions when validating

Have been trying for weeks, but my App won't ask for extended permissions, obviously that means that it wont POST to the timeline afterwards either.
require_once('/wp-content/php-sdk/src/facebook.php');
$facebook = new Facebook(array(
'appId' => 'xxxxxx',
'secret' => 'xxxxxxx',
));
$user = $facebook->getUser();
if ($userID) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'www.theaandrdepartment.com/raw',
'message' => 'I just helped an Aussie band make it to
radio play! By voting, you can too.'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
} else { $login_url = $facebook->getLoginUrl(array(
'scope' => 'publish_stream'
));
header("Location: ".$login_url);
}
If anyone could help that would amazing.
I copied most of this code straight from the Facebook example, but to no avail.

Facebook. Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user

with my app's administrator acount on facebook my app work normally, but with other account I get error: Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user.
I had this problem before with other app (publish text on the user's wall), but fixed after i added
$user = $facebook->getUser(); What's wrong here? I have added offline_access permission... Help me, please if you can, Thank you very much.
<?php
require_once('images/Facebook.php');
$facebook = new Facebook(array(
'appId' => '456080124457246',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
));
$user_profile = $facebook->api('/me','GET');
$accessToken = $facebook->getAccessToken();
# Get User ID
$user = $facebook->getUser();
$facebook->setAccessToken($accessToken);
$facebook->api(456080124457246);
if ($user) {
try {
# Photo Caption
$photoCaption = $user_profile['name'] . ' patarimų plaukams sužinojo čia http://goo.gl/otwhf';
# Absolute Path to your image.
$imageUrl = 'http://padekime.wu.lt/plaukai/images/PlaukaiNeuzvedus.jpg'; // Example URL
# Post Data for Photos API
$post_data = array(
'message' => $photoCaption,
'url' => $imageUrl
);
$apiResponse = $facebook->api('/me/photos', 'POST', $post_data);
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload'
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
?>
In this case it's not even getting to your $facebook->getUser() call -- it's throwing an exception as soon as it reaches this line:
$user_profile = $facebook->api('/me','GET');
$facebook->api() is a bit of a tricky thing to work with because it throws an exception immediately if it doesn't know who "/me" is...even if you try to fix it later.
The trick, I think, is to wrap the entire thing in a try...catch block. Like so:
<?php
require_once('images/facebook.php');
$facebook = new Facebook(array(
'appId' => '456080124457246',
'secret' => 'xxxxx',
));
try {
$user_profile = $facebook->api('/me','GET');
# Get User ID
$user = $facebook->getUser();
if ($user) {
try {
# Photo Caption
$photoCaption = $user_profile['name'] . ' patarimų plaukams sužinojo čia http://goo.gl/otwhf';
# Absolute Path to your image.
$imageUrl = 'http://padekime.wu.lt/plaukai/images/PlaukaiNeuzvedus.jpg'; // Example URL
# Post Data for Photos API
$post_data = array(
'message' => $photoCaption,
'url' => $imageUrl
);
$apiResponse = $facebook->api('/me/photos', 'POST', $post_data);
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload'
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
} catch (Exception $e) {
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload'
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
?>
That'll redirect the user to the login url pretty much immediately, then come back with everything in tow. You don't have to set the accessToken with this setup.
This may actually unnecessarily repeat some functionality, but hopefully it's something to start with.
By the way, for what it's worth the offline_access permission is being phased out.
I stopped using "me" keyword to get the logged in user's profile.
instead of $facebook->api('/me','GET'), I changed to $facebook->api('/the facebook ID of the user','GET');
this reduce the need to do second try catch
If you do
if (!empty($user)) {}
That should help...

Facebook application : Doesn't redirect to OAuth in an iFrame

I am currently developing a facebook application using the php sdk and the js sdk. (I need both of them)
When a user has not yet authorized the application and loads the canvas page (https://apps.facebook.com/app_name/), nothing happens, he just gets a blank page with the facebook bar on top.
But if he loads the direct url of the application (https://my_app.mydomain.com), it works fine and he is redirected to the OAuth Dialog. If he authorizes the application, he is then redirected to https://apps.facebook.com/app_name/ and the iframe loads correctly.
If the user has already authorized the applications, both links are working (https://apps.facebook.com/app_name/ and https://my_app.mydomain.com).
Here is my authentication code :
<?php
require 'php-sdk/facebook.php';
$facebook = new Facebook(array(
'appId' => '327991330620101',
'secret' => '79asecretcc4300',
'cookie' => true
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$params = array(
'scope' => 'publish_stream',
'redirect_uri' => 'https://apps.facebook.com/quiet_quies/'
);
$loginUrl = $facebook->getLoginUrl($params);
header('Location: ' . $loginUrl);
}
Does anybody have an idea about this ? It's really driving me mad...
Thanks for you help.
yes, #CBroe is right, in your php code try replacing header('Location: ' . $loginUrl); line with
echo "<script language=javascript>top.location.href ='".$loginUrl."'</script>";
it should work then.

Facebook permission request, endless redirect loop

I have this code:
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxxxx',
'secret' => 'yyyyyyy',
'baseUrl' => 'http://xxx.yyy.zz/',
'appBaseUrl' => 'http://apps.facebook.com/xxxxxx/',
'fileUpload' => 'true',
));
$user = $facebook->getUser();
$params = array(
scope => 'publish_stream,user_photos',
redirect_uri => 'http://www.facebook.com/xxxxxx?sk=app_123456789'
);
if ($user){
....
}
$loginUrl = $facebook->getLoginUrl($params);
if ($user){
// nothing
}else{
echo "<script type=\"text/javascript\">top.location.href = \"".$loginUrl."\";</script>";
}
My problem is, that it gets into an endless loop... reloads the page over and over.
Can someone help me to fix this?
Thank you very much!
I think the problem is your redirect_uri. Try to define the real path to your url (www.yourdomain.com/app/index.php) instead, since Facebook will send a POST (including a code) to that url and won't pass it with the app data or signed request to your iFrame application.
The problem was that the main logic was not int the index.php. After i put everything back into that, the redirection stopped.

Facebook application - server not responding

After the FBML problems I switched to iFrame apps. All well for now except that 2 things:
1) if I did not entered in the app for a few hours and I try to enter, sometimes it gives me the error page that my server did not responded. It shows the Faceboob page with iFrame inside showing the error. After 1 or 2 refreshes it's working fine.
2) I implemented the new SDK with login like this
$facebook = new Facebook(array(
'appId' => $fbID,
'secret' => $fbSecret,
'cookie' => true,
));
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
d($e);
$user = null;
}
}
if (!$user) {
echo "<script type='text/javascript'>
top.location.href = '$loginUrl';
</script>";
exit;
}
The problem is my first redirect, when user enters for the first time. It redirects to my server URL not my Facebook app URL. If I try to put it like this:
$loginUrl = $facebook->getLoginUrl(
array(
'redirect_uri' => 'http://apps.facebook.com/xxx/'
)
);
the login is entering in an infinite loop.
Any help solving this two problems is welcome:-)
Thanks
Darko4spain when the user is redirected back to your site, you can detect if is in iframe with javascript and send them back. I have same issue with my app. This is not a pretty fix but it works.
if (window!=window.top) {
//FB.Canvas.setAutoResize();
setTimeout("FB.Canvas.setAutoGrow()", 1400);
}else {
top.location.href = 'https://apps.facebook.com/AnotherFeed/?ref=redirect#comments';
}
example: https://anotherfeed.com/plugins/index.php will redirect you back to my canvas if you are not in the canvas.