Facebook SDK : Sending Request from Page Tab App - facebook

I can send requests to friends using "Multi Friend Request Selector" inside my Page Tab App, which link looks like :
https://www.facebook.com/<my_page_name>/app_<my_app_id>
My friend receives my request, and click "Accept". It forwards to the real link of the Facebook App, which is :
https://apps.facebook.com/<my_app_name>/?fb_source=request&request_ids=<an_request_id>
which is not inside a Page Tab anymore. How can I forward to user to the 1st link instead of the 2nd while the request_ids have to be kept? Do I just make a header('Location: url'); forwarding?
p.s. Timeline is already in use.

Yes you have to forward to your page url, because requests always redirect to the canvas url.
To pass the request ids you can use the app_data field. So instead of redirecting to https://www.facebook.com/<my_page_name>/app_<my_app_id> you redirect to https://www.facebook.com/<my_page_name>/app_<my_app_id>?app_data=requestids. Here requestids are the actual values(the comma separated list that the canvas gets).
app_data is available to your page tab app as part of the signed_request it receives.
From docs:
In addition, your app will also receive a string parameter called app_data as part of signed_request if an app_data parameter was set in the original query string in the URL your tab is loaded on. For the Shop Now link above, that could look like this: "https://www.facebook.com/YourPage?v=app_1234567890&app_data=any_string_here". You can use that to customize the content you render if you control the generation of the link.

Related

is it possible to share whole Fb app Canvas page through app?

I'm working on a photo contest fb app to run in a fan page tab. the user should be able to share the photo in order for others to vote for them.
supposing image link in iframe is http://example.com/image.php?id=1 for particular photo, pressing share will share this link through iframe. which leads up to the host app itself.
what I need is sharing the whole fb app tab page url with http://example.com/image.php?id=1 open in its Iframe.
is that possible in any way?
thanks for help.
So to give the “alternative” to #Lix’ answer, which focuses on canvas apps, here the analog way for page tab apps:
For some reason Facebook decided to do things differently for page tab apps – different than with canvas apps, you can not pass just any GET parameters to your app by appending them to the facebook.com address of your app, but you have to use the app_data parameter for that.
You call/link to your app in the form https://www.facebook.com/YourPage?v=app_1234567890&app_data=foo – and whatever you put as value for the parameter app_data, you will find in the signed_request parameter that Facebook POSTs to your app on initial(!) load into the iframe.
So you parse the signed_request (or let f.e. the PHP SDK do that for you), and then you find the app_data value in there. If you want to pass more than one single value, you can f.e. also put JSON-encoded data there – then you have to decode that again after you read the app_data value from the signed request.
The docs just shortly mention the app_data parameter, but the principle itself is quite simple.
Now, when it comes to sharing those links, I found that when you use an address in the above form, Facebook tends to cut the parameters from the URL, and treat the whole link as just a link to your Facebook page – it shows the page’s picture and description, and does not even pass your page tab app along, let alone the app_data parameter.
I found the most reliable way around this is not to link to your page tab on Facebook directly, but instead to a URL of your own app. When the scraper visits it, you deliver the relevant OG information. And when it’s a real user visiting, you redirect them to your page tab app, passing the data you need via the app_data parameter as described above. Redirecting can either be done server-side (info on how to detect the scraper server-side via its User-Agent header), or client-side via JavaScript (which the scraper does not “speak”).
Sure it is. All you have to do is be able to extract the information from your application canvas URL. If your canvas URL is something like this:
https://apps.facebook.com/ImadBakir
Then you could place some more info in there, like this:
https://apps.facebook.com/ImadBakir?photo_id=123
Users will share that link and now in your application, you can parse that photo_id parameter and make the needed HTML changes to display the correct image inside your iframe as the page and application loads.
With regard to parsing the the URL parameters, assuming you'll be doing it with JavaScript, you can read more about it in this post:
How can I get query string values in JavaScript?

Prevent Facebook Tab App from running outside of iframe

I developed a facebook fan page app with PHP SDK and am successfully running it. I have a Fan Gate implemented, that is, users have to like the page before they can use the app.
What I could not solve until yet is this: I would like to prevent my app from running outside of the Facebook iframe. As of now my app can run in a new browser tab or window, when a user copies and pastes the URL (of the iframe src). At this point I'd like to give an error message like "This app can only run in a Facebook tab".
I am catching the signed_request but as my app has subpages and routines, I have to store the signed_request in a session variable.
Any advices?
Ok, so what I usually do is provide data in the app_data field of signed_request to know which page to load. When POSTing, I redirect to the tab directly after processing the payload.
So, there are two methods a page, that is actually a facebook tab, may be requested: GET and POST.
GET
When using GET, the tab should always be requested using a facebook URL. For example: The tab's subpage "form" should have a URL like: http://www.facebook.com/pages/xXx/[page_id]?sk=app_[app_id]&app_data=form
In your server-side code you recognize the data in app_data after decoding the signed_request (see 1 and 2).
In your links to the form page, you also have to add the target="parent" attribute.
By this, facebook is reloaded and the tab feels slower than when you directly link the subpages. But you have URLs that identify a subpage, which is what we prefer.
Additionally, you can expect every GET request to have a signed_request. If it doesn't, it is not called via facebook and you can either show an error message or, what I suggest, redirect to you tab (sub-page).
POST
POSTs never have a signed_request. But usually, when surfing, browsers use GET requests. So you can assume, if you receive a POST request, it is either from your own form or a hacking attempt.
In both cases, you check the values of that request for validity.
If they are valid, you save them to your DB and redirect to a "success" page. E.g. if you are up for a contest, you should show a page that assures the user he/she has successfully participated.
If the data is not valid, you save your error messages into the session and redirect back to the form where you show them.
I always suggest you redirect after a POST request, no matter if you are in a facebook tab or not. But in a facebook tab, the redirect must be done using a javascript since it is within an IFRAME and you want the whole page to reload:
<script type="text/javascript">
parent.location.href = 'http://www.facebook.com/pages/xXx/[page_id]?sk=app_[app_id]&app_data=form';
</script>
Post scriptum 1: For me, forms always use POST requests.
Post scriptum 2: If you don't like the idea of the whole facebook "frame" to be reloaded every click, you can think of doing some AJAX magic where the requests are "spiced" with extra data for you to recognize them as "yours".

How to get the direct link to a facebook iFrame url?

I have a app which url is like:
https://www.facebook.com/Foo/app_123456789
this app is running in a iframe and loading some urls of my server. At the start the user see a page like[1]:
www.foo.com/index.php
but in the app you also get pages like[2]:
https://foo.com/foo1/test/Tab/New/12345-6789
now the question:
is it possible to get a link which start with facebook.com/... and directly lead you to the [2] url?
Yes it is possible. You can add an extra variable to your URL like so:
https://www.facebook.com/Foo/app_123456789?app_data=any_string_here
Facebook will then send whatever inside the app_data variable to your iframe inside the singed_request. So all you have to do is to check for its presence (and value) and redirect the user accordingly.

Facebook fan page app pass string to app_data and retrieve

Hi i have a multiple page facebook fan page app in asp.net c#. how do i set and retrieve that app_data querystring? is this part of the signedrequest or the actual url of the fan page? will there be cross domian issues in getting this in safari and ie?
i can get the signed_request using the following:
if (!string.IsNullOrEmpty(signedRequest))
{
dynamic SignedRequestData;
var DecodedSignedRequest = FacebookWebContext.Current.SignedRequest.Data;
SignedRequestData = DecodedSignedRequest
so basically on the first page i want to get the signed_reqeuest using the above and then append this to the url in app_data querystring. on all other requests i can then get this and use those values to check user status, liked, logged in etc. This way i think it will stop the cross domain cookie issues in safari. anyone agree or disagree with this?
has anyone done this?
cheers
You can set the app_data as:
https://www.facebook.com/{page_id}?sk=app_{app_id}&app_data={"key1":"value1","key2":"value2"}
You can pass multiple values into app_data and it will be returned in the signed_request to your actual page as (POST data).
What you should really do is get the signed_request on first page load from POST, and add it to the URL of your app as ...?signed_request={signed_request_string}. Every page should then pass this around in the URL so you can easily check the like and admin status from any sub-page. You don't need to use app_data for this. And in any case, the data will be lost after the any subsequent click / redirect in your app.

How to share links to different pages on iframe?

I have an iframe tab which holds php page with various links inside (like "News", "Photos" etc.). Is it possible to generate a facebook link which leads directly to certain link inside the iframe? By facebook link I mean link that will keep you on facebook, open your iframe tab and view certain page on that iframe. The thing is that all I can do now is to link only to my iframe tab which has address like http://www.facebook.com/mypage/app_someid. Then I will land on my default page tab url. But I want to land on different one. Any tips on this one?
Passing an app_data parameter to the tab URL is passed back to your application as part of the signed_request parameter in FB's post call to include your content. Use that parameter after de-signing it to then decide what to show and what not to.
Your URLs will become like:
http://www.facebook.com/mypage/app_someid?app_data=showViewX
http://www.facebook.com/mypage/app_someid?app_data=showViewY
and then in your code:
// Get Signed Request
$app_data = $signed_request['app_data'];
if($app_data === "showViewX") {
// Show one view
} else {
// show others..
}
Currently, the app_data param is not sent if the user just liked the page when he lands on it with an app_data param. The bug on FB has been filed at https://developers.facebook.com/bugs/172963266154996 and we are all hoping for an early resolution.