Get Facebook Page Tab ID of current Tab? - facebook

I am trying to figure out how to get the tab ID of the current tab of a Facebook Page the user is visiting.
I have made an app for installation on Facebook Pages, that I need to save settings for, per instance. I have figured out that you can get an array of tabs for the page the app is installed on, but I can't figure out how to get the tab ID for the actual tab you're on.
The point is for an admin to be able to save settings for each tab that they've added the app to, using a single backend. I'm not sure if you can have multiple instances of one app on the same page, but if not, we'd have 2-3 duplicate apps with the same backend in the iframe. Because of that, I need to be able to identify app installations as unique - the best way I can figure out is through using the page id and tab id, for the app.
How do I do that?
UPDATE: Found out that you can only have one instance per app on a page.
With that, I went with using this solution (with '/tabs/' to get the tab info):
try {
$tab = $facebook->api('/'.$fb_page_id.'/tabs/'.$fb_app_id);
$fb_tab_link = $tab['data'][0]['link'];
} catch (FacebookApiException $e) {
echo '<!-- '.htmlspecialchars(print_r($e, true)).' -->';
}
In the above code, $fb_tab_link or a combination of $fb_page_id and $fb_app_id can be used as unique identifier. I decided to use the concatenation $fb_page_id . '-' . $fb_app_id as the instance ID.

After some research, I found the call to get the tab information via the API.
try {
$tab = $facebook->api('/'.$fb_page_id.'/tabs/'.$fb_app_id);
$fb_tab_link = $tab['data'][0]['link'];
} catch (FacebookApiException $e) {
echo '<!-- '.htmlspecialchars(print_r($e, true)).' -->';
}
Apparently, the tab ID is the combination of Page ID and App ID, which means that you cannot install the same app to more than one tab on a page.
I hope this can be helpful to someone who needs to find the unique ID for a tab on a Facebook Page.

In facebook api, when you want to change the tabname, you need to pass the tab id. see the example below.
To change your tab name using Javascript SDK.
your application tab_id will be app_[AppId].
FB.api([PageId]/tabs/[tab_id]', 'post',
{access_token: [page access token], custom_name:[custom tab name]},
function(response){
if(respose == true)
console.info("Successfully done");
}
);

Ok Calle thanks for the confirmation!
I was trying to figure out this too... and it makes sense... That's why when you create a tab programmatically for a page the only parameters are app_id & access_token.
If you try to create an app tab twice on the same page you'll get "true" which in my opinion means that you overwrite last tab you have created.

Facebook adds a signed_request parameter to the request URL of your app. You can decode this parameter to obtain the page ID, among other useful information:
http://developers.facebook.com/docs/authentication/signed_request/

Related

Facebook share to refer back traffic to the iframe page tab

SO here's what I am after. I have a FB page tab that runs the content of the site https://site.com/
I set up a FB share link to share a page aboutus.html. When I share it FB allows me to share this URL https://site.com/aboutus.html, but how can i send the traffic directly to the iFrame on the page tab? For example https://www.facebook.com/fan_page/app_331267943480920398/whatever_aboutus.html
I know it is possible because I saw it one day - cant remember now where.
Thanks.
You can't pass in filenames this way, that's only supported on Canvas Apps.
The best workaround to replicate this is using the app_data parameter. Basically, have your landing page (as defined in your app settings), be some kind of server side script which listens to the Signed Request, specifically the app_data parameter. Then you have that script load content based on the contents of that.
So as an example, let's imagine I want to load http://mybasedomain.com/foo.php or http://mybasedomain.com/bar.php. I direct users to https://www.facebook.com/PAGENAME/app_APPID?app_data=foo or https://www.facebook.com/PAGENAME/app_APPID?app_data=bar, then on my landing page, I have a simple if/else statement to parse that and render the relevant content -
<?php
// Assumes you have this parse_signed_request function - https://gist.github.com/872837
// And a $config array, which contains your app_secret
$signed_request = parse_signed_request($_REQUEST['signed_request'], $config['AppSecret']);
if ($signed_request['app_data'] === 'foo') {
include('foo.html');
} else if ($signed_request['app_data'] === 'bar') {
include('bar.html');
} else {
include('index.html');
}

Why I cannot get facebook like my page?

What I want to do is user must like my page first then only can go to my facebook apps, but I have no idea why I cannot detect it and I had var_dump my page liked, result is NULL
here is my code
$request = $_REQUEST["signed_request"];
list($encoded_sig, $load) = explode('.', $request, 2);
$fbData = json_decode(base64_decode(strtr($load, '-_', '+/')), true);
var_dump($fbData["page"]["liked"]);// output is NULL
if (!empty($fbData["page"]["liked"]))
{
echo "This content is for Fans only!";
} else {
echo "Please click on the Like button to view this tab!";
}
Any idea how to do it?
Have you made sure there is as signed_request parameter?
It only gets transmitted to your app once the page is initially loaded into the Facebook iframe – it won’t be there anymore once you start navigating inside your app.
Best thing to do IMHO is to parse the signed_request if not empty, and safe the results into your own session.
Btw., parsing it yourself is rather time-consuming – I’d suggest using the PHP SDK and use Facebook::getSignedRequest() – that method gives you all the data in decoded form, if there was a signd_request parameter.
So here’s what I usually do:
if($signed_request_data = $Facebook->getSignedRequest()) {
$_SESSION['signed_request_data'] = $signed_request_data;
}
You can call that code on every page – only if the method actually returns data, your session gets updated.
You need the permission user_likes to check if they liked your Facebook App but $fbData["page"]["liked"] is referring to your Facebook App Page which is seperate.
To edit your App Page go to App Settings > Advanced > Near the bottom you will see App Page
To use the permission user_likes to check see this SO Answer On Detecting if a User Likes a Facebook App

Change tab name facebook page

Is there a way to change the tab name of facebook page by
facebook graph API using PHP SDK. I think it is constant during
setup of facebook application.
after setting tab name of an application can I change tab name of facebook
page later by graph API.
Yes you can do this using the graph api.
First you have to get an access token for the page, to do this you will need to ask for the manage_pages permission.
Once you've initialized the PHP SDK to get the the page access token you make a call to...
$facebook->api('/THE_PAGE_ID?fields=access_token');
This returns an array like below
{
"access_token": "THE_PAGE_ACCESS_TOKEN",
"id": "THE_PAGE_ID",
"type": "page"
}
Before you use this token you may want to save the current users access token so you can set set it back again once you've finished doing the page stuff.
$users_access_token = $facebook->getAccessToken();
To then make the SDK use the page access token for the next call do
$facebook->setAccessToken('THE_PAGE_ACCESS_TOKEN');
Now we're ready to change the tab name by making a POST to /THE_PAGE_ID/tabs/TAB_ID with an array containing the new custom name
$this->api('/THE_PAGE_ID/tabs/TAB_ID', 'POST', array(
'custom_name' => 'THE_NEW_TAB_NAME'
));
TAB_ID should be the tab apps id prepended by app_
That should be it, don't forget to set the access token back to the users token before you try do anything non tab related
$facebook->setAccessToken($users_access_token);
It might be worth noting that you can use exactly the same process to change the tab position and set the default landing tab, you just need to add a couple of extra bits to the POST array, 'position' and 'is_non_connection_landing_tab'
$this->api('/THE_PAGE_ID/tabs/TAB_ID', 'POST', array(
'custom_name' => 'THE_NEW_TAB_NAME',
'position' => 1,
'is_non_connection_landing_tab' => true
));
Have a look here http://developers.facebook.com/docs/reference/api/page/ scroll down to Tabs. Graph api provides Read, Create, Update, Delete functionality.

Cannot get page "like" status using Facebook C# SDK

I am using the latest Facebook C# SDK (v5.0.40 at time of writing) from here: http://facebooksdk.codeplex.com/.
I have created a test iFrame app in Facebook and got the CSASPNETFacebookApp sample running so that it displays the name of the currently logged in user within Facebook.
What I would now like to do is display different content in my iFrame depending on whether the user has "liked" the page. However the signed_request never seems to contain that information. From what I can see in the FacebookSignedRequest.cs I will only get the payload which contains the page information if the algorithm is AES-256-CBC HMAC-SHA256 but I only ever get HMAC-SHA256 returned.
What am I doing wrong? How do I get it to return the page information? Is it a problem with the setup of my app in Facebook or a configuration issue with the .NET app?
Thanks in advance.
var fb = new FacebookClient("access_token");
dynamic parameters = new ExpandoObject();
parameters.method = "pages.isFan";
parameters.page_id = "{you page id}";
dynamic result = fb.Get(parameters);
bool isFan = result == true;
Neil Knight's answer is technically correct, you can use FQL to look up whether a user has liked a page and it did help to set me on the right path. However my issue was actually more one of set up rather than code. What I didn't understand is that you only receive the "like" information in the signed request if your app is running within the "context of a page". If you set it up correctly then Facebook will pass your app the like flag without the user needing to "Allow" your app.
The steps are:
(1) Create your iframe application in Facebook
(2) Set up a tab url for your app in the app settings. This is what the parent page will use when it generates a link in the left hand column to go to your app.
(3) Go to your "App profile page", the URL will be something like this: http://www.facebook.com/apps/application.php?id=12345 Where 12345 is your app ID.
In the left hand column below the logo image there should be a link "Add to Page". If you click on that link you will be presented with a list of pages that you are the admin for. Select the page you want to like in your app.
Now if you navigate to your page you should get a link to your app in the left hand column. It is only when clicking on that link that you will get the page id and like status passed through to your application.
Hope this helps someone having the same issue.
You could use FQL in order to achieve this. I have just done this using the following statement:
var the_query = FB.Data.query("SELECT uid FROM page_fan WHERE page_id = {0} and uid={1}", page_id, user_id);
In order for this to work, I had to ask the user to "Allow" my application so that I had permission to check to see if they liked the page. Then, it was a simple case of checking the result and displaying the necessary <div>:
the_query.wait(function (rows) {
if (rows.length == 1 && rows[0].uid == user_id) {
$("#myLikeContent").show();
} else {
$("#myNoLikeContent").show();
}
});

Facebook fan page tab and user id

As per the documentation in the following link, we can get the user id if the uer will interact with the form..
http://wiki.developers.facebook.com/ind … d_Policies
"If a viewing user interacts with the tab (like submits a form, takes an action that causes an AJAX load of new content, or follows a relative URL that loads on the tab), that user's UID is sent to the application as the fb_sig_user parameter, the profile owner's user ID is sent as the fb_sig_profile_user parameter. The viewing user's session key is key is sent only if the user authorized the application. "
In my fan page tab am I have an AJAX form which the user can submit with some value.. now I need the users id also.. how can I get this..
I tried to get the value in my AJAX submit page using $_POST['fb_sig_user'] with no success.. can anyone help me with this please..
You won't be able to get the id of the user using $_POST['fb_sig_user'] unless you authenticate the user by having this in the facebook's ajax function:
ajax.requireLogin = true;
For example, I'm retrieving it fine with this:
function do_ajax(url, div_id)
{
document.getElementById('poller_waitMessage').setStyle('display', 'block');
var ajax = new Ajax();
ajax.responseType = Ajax.FBML;
ajax.onerror = function(error)
{
new Dialog().showMessage("Error:", "Some communication error occured, Please reload the page.","Ok");
};
ajax.ondone = function(data)
{
document.getElementById('poller_waitMessage').setStyle('display', 'none');
document.getElementById(div_id).setInnerFBML(data);
}
ajax.requireLogin = true; // <----- this is important
ajax.post(url);
}
I've been happily using the form variable fb_sig_profile_user for previous apps, and when developing a new app last week, the variable was no where to be found.
Searched for several days, I was about to give up, and then the found answer:
ajax.requireLogin = true;
I understand FB cares about privacy and all, but they really need to announce these kinds of changes before just taking it away.
Million Thanks!