Facebook how to keep facebook session - facebook

I hope that someone can help me. I am developing a Iframe application. I am using a mixture of PHP and Javascipt.
Everything works perfectly if you click on the facebook tab created in the facebook profile. However I have a second page which edit the some functionality in my page, If I navigate to this page, it seems to loose it's authentication, or login session.
The like button, goes to not-liked - (so it seems to me the session breaks).
How can I keep the facebook session, to the next page?
I have included the complete page of my nav.php (This is the page that facebook first access)
<?php require 'facebook.php';
$app_id = "MY-APP-ID";
$app_secret = "MY-APP-SECRET";
$apiKey = "1573bcaff76885d24c5ca93d93266a9e";
$app_url = "http://domain.com";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
if ($_SERVER['HTTP_REFERER'] != 'http://domain.com/facebook/edit_page.php');
{
$signed_request = $facebook->getSignedRequest();
$page_id = $signed_request["page"]["id"];
$page_admin = $signed_request["page"]["admin"];
$like_status = $signed_request["page"]["liked"];
$country = $signed_request["user"]["country"];
$locale = $signed_request["user"]["locale"];
if ($page_admin == 1) {
$oauth_token = $signed_request["oauth_token"];
}
}
if ($like_status) {
// If you like the page this will show - START
}
else {}
if ($page_id == '12345') {
echo "content for 12344";
}
elseif ($page_id == '56789') {
echo "content for 56789";
} ?>
In this page I can see who is the owner of the page, and the LIKE button shows liked! how do I pass the authentication to a next page? so that the Like button stay Liked?
Any help would be appreciated!
Thanks

I have figured out the problem - In my case I needed to pass the getSignedRequest(); to all pages, for the session to stay in place.

Related

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 application access for the public

Ive created my first facebook app. Basically the canvas/iframe version.
It all seems ok ..except:
I can only access the apps page: (http://apps.facebook.com/hoo_promo/) when im logged in as the administrator user of the app. It should be viewable by everone. Ive submiited it to the appstore...does this matter? is this the issue?
My like-gate doesnt seem to work, even though ive followed several examples. My admin user, who im logged in with, has the page liked, but the code
$signed_request = $facebook->getSignedRequest();
$like_status = $signed_request["page"]["liked"];
for page-like is never set in the signed_request obj.
Any help much appreciated....ive added my current code...maybe this can also help others...
$app_id = "secretblahblahhiddenthis";
$secret = 'secretblahblahhiddenthis';
$canvas_page = "http://www.houseofoak.co.uk/facebook_app/";
$auth_url = "http://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" . urlencode($canvas_page);
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $secret,
'cookie' => true,
));
$signed_request = $facebook->getSignedRequest();
$like_status = $signed_request["page"]["liked"];
$user = $facebook->getUser();
$page = 'unliked';
$loginUrl = $facebook->getLoginUrl(array('scope' => 'user_likes'));
$logoutUrl = $facebook->getLogoutUrl();
if (($user) && ($like_status)){
$page = 'liked';
}
For your problems:
Check to make sure you aren't in sandbox mode. Visit https://developers.facebook.com/apps/YOUR_APP_ID/advanced to check.
What you're trying to do is no longer allowed. Facebook has deprecated most of the methods that make a like gate possible. See Facebook Platform Policies Section IV. Point 1 says "You must not ... gate content behind ... Facebook social channels...."

facebook app oauth error (can not be displayed in frame)

I just wanted to create a FB app. I went to developers.facebook.com/apps, set up a new app, created a new folder on my server, copied an old (working) app into the folder, changed the app id, secret, canvas page and canvas url in my configuration file... and it did not work.
It displayed an error: "this content can not be displayed in frame".
I would be grateful if you could help me. Thanks.
I copy here my config file: (I intentionally removed the app id and secret here.)
// Facebook App ID/API Key
$appId = '';
// Facebook App Secret
$secret = '';
// Facebook Canvas Page
$redirect_uri = 'https://apps.facebook.com/appname/';
// Facebook Canvas URL
$canvas_url = 'https://myhost.com/appname/';
// Facebook App Permissions
$scope = 'email, publish_actions';
// Configure Facebook connection
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $secret,
'cookie' => true,
));
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(array(
'redirect_uri' => $redirect_uri,
'scope' => $scope,
));
$logoutUrl = $facebook->getLogoutUrl();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
} else {
header("Location: $loginUrl");
}
You should use JavaScript to redirect the user to the login URL using top.location = '{url}', and then use the login redirect url to send them back to your application. Facebook doesn't allow you to use facebook within an iframe, that why you are seeing that error message - pretty self explanatory.
It displays a "this content can not be displayed in frame" errormessage.
And what about this message gives you trouble understanding?
You can't call the auth dialog in an iframe, because it would enable phishing if the user can’t check the sites address.

Redirect back to page tab after user authenticates?

How should I go about redirecting the user back to my page's tab after they authenticate my app? I cannot put one specific url in for the redirect since my app will live on multiple pages. So somehow I need to grab the page's id and put it into the url. I've tried to use session variables but it doesn't seem to be working for me. :( Here's a portion of my code...
$signed_request = $facebook->getSignedRequest();
$_SESSION['TrueID'] = $signed_request['page']['id'];
$fbconfig['appBaseUrl'] = "http://www.facebook.com/pages/".$_SESSION['TrueID']."/".$_SESSION['TrueID']."?sk=app_241321439259320";
/*
* If user first time authenticated the application facebook
* redirects user to baseUrl, so I checked if any code passed
* then redirect him to the application url
* -mahmud
*/
if (isset($_GET['code'])){
header("Location: " . $fbconfig['appBaseUrl']);
exit;
}
//~~
//
if (isset($_GET['request_ids'])){
//user comes from invitation
//track them if you need
}
As you can see I'm trying to set a session variable to grab the page's id.. but that's not working for me :( The variable echo's out just fine when I visit my page.. but I'm guessing its getting lost somewhere during the authentication.
So when a Page add/install your app, you should store the page's link along with the page's id.
Now when your page tab is loaded, Facebook will send the page parameter which will contain the page id (along with other info, refer to the documentation). You retrieve that id, get the page's link from your db and construct your page tab link, which would be something like (where $page is the page's db record):
$redirect_uri = $page['page_link'] . '?sk=app_' . $APP_ID
Since you are using the PHP-SDK, this is how you construct your login:
$user = $facebook->getUser();
if(!$user) {
$url = $facebook->getLoginUrl(array('redirect_uri' => $redirect_uri, 'scope' => 'read_stream'));
echo "<script>top.location.href = '$url';</script>";
exit;
}
Of course you may not want to redirect to the login directly but instead have a call to action link:
Connect
Best way I found is to set the Site URL in the app settings to http://www.facebook.com/pages/
Then do something like this(tested):
$uid = '';
$facebook = new Facebook(array(
'appId' =>'xxxxxxxxxxxxxxx',
'secret' =>'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'cookie' => true,
'oauth' => true,
));
$uid = $facebook->getUser();
$signedrequest = $facebook->getSignedRequest();
$page_id = $signedrequest["page"]["id"];
$fb_access_token = $signedrequest["oauth_token"];
if($uid==''){
echo '<div id="authenticate">Authorize App</div>';
This might be a little bit of a hack, but it works well for my situation:) Hope it helps someone!

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.