Facebook API - how to get page ID using Facebook API? - facebook

With Facebook API, within a custom app added to a fanpage (tab app), how do I get the fanpage ID?
Here's what I'm trying to achieve:
user goes to the full-width canvas app, he authorizes himself there and allows the app to add a tab to one of the fanpages he's administrating
every Facebook user going to that fanpage, sees the tab and the tab shows content specific to that fanpage (that's why I need to know the fanpage ID)
if fanpage administrator wants to change something, he goes to the full-width canvas and logs in to my service (here's where I store what he wants to show in his Fanpage)

From a parsed signed request (to an tab iFrame) you can get the page id - the contents would be similar to the following (output of PHP print_r):
stdClass Object
(
[algorithm] => HMAC-SHA256
[issued_at] => xxxxxxxxxx
[page] => stdClass Object
(
[id] => FAN_PAGE_ID // target page id
[liked] => 1 // is the user a fan
[admin] => // is the user an admin
)
[user] => stdClass Object
(
[country] => ie
[locale] => en_GB
[age] => stdClass Object
(
[min] => 21
)
)
)
For more information see:
http://developers.facebook.com/docs/authentication/signed_request/

Related

Get user id in case Signed Request - Facebook

I know it might be duplicate question but please help. I have created a app and installed it on my Facebook page. My issue when I authenticate it using authentication method I got the user id... But when I go through proper signed request method (i.e. When I login with facebook site only.) I don't get user. how can I get userid in case of signed request that When I login using facebook site.
My signed request.
$facebook = new Facebook(array(
'appId' => '****',
'secret' => '****',
'cookie' => true,
));
echo 'User ID :- ' . $user = $facebook->getUser();
$signed_request = $facebook->getSignedRequest();
echo '<pre>';
print_r($signed_request);
echo '</pre>';
User ID :- 0
Array (
[algorithm] => HMAC-SHA256
[issued_at] => 1372676199
[page] => Array
(
[id] => 544896778905441
[liked] =>
[admin] => 1
)
[user] => Array
(
[country] => in
[locale] => en_US
[age] => Array
(
[min] => 21
)
)
)
How can I get User id.
** EDIT **
As of a few months ago (Dec 2014) Facebook no longer allows Like-Gating content. I guess that's why someone is going around placing negative votes and links to their own answers. So the below is no longer valid
There is a difference between getting a signed_request for a user who has authorised an app, which is your first case and getting the user id from a signed request on a page app. In the second example you can test if a user likes your page, but you can't get the specific user id. They are just interacting with your page - yes they may be logged into Facebook, but they haven't explicitly authorised your app. I presume this is what you mean by "login with Facebook site only"
If you look at https://developers.facebook.com/docs/reference/login/signed-request/ you will see it states
Some fields and values, the user_id and oauth_token for example will only be passed if the user has logged into your app.
They may be logged into Facebook, but not have logged in/authorised your app
You can follow the instruction on how to decode the signed request here
https://developers.facebook.com/docs/facebook-login/using-login-with-games#parsingsr
OR
You can use the service that I created. It will return decoded signed request :)
https://websta.me/fbappservice/parseSignedRequest/<concat signed request here>
//angularjs
you may get it via $http.jsonp(url);
//jquery // refer to jquery docs on getting jsonp requests
$.getJSON( url, {
format: "json"
})

One FB application to more than one FB Page

I want to add a an FB application to more than one FB Page.After the user's authentication , rediraction to my application's url I can't find a way for the application to know the page which the request was made. Thank you in advance for your help
You will get the page id in the signed request.
FYI: Signed Request
This is pretty easy to do. If you decode the signed_request data POSTed to your page, you can see what page is 'looking' at your app (i.e. which page the app has been installed on).
If you decode the data (e.g. in PHP you can do: print_r( $facebook->getSignedRequest() ); to print the decoded version), you will see something like:
Array
(
...
[page] => Array
(
[id] => 1234567890
[liked] => 1
[admin] => 1
)
[user] => Array
(
...
)
)
The $response['page']['id'] is the ID of the Page that is looking at your app at that moment. You can store the IDs of the page that have installed your app, and check it with this to determine which content to load up.

Facebook application timeline tabs

I have a question about the timeline tabs. I have created an application which has been installed on 2 different business pages as tabs. Now what I want to do is change the content of the tab based on which business page is being viewed. Another company has done this but I can't work out how. Any thoughts?
This is pretty easy to do. If you decode the signed_request data POSTed to your page, you can see what page is 'looking' at your app (i.e. which page the app has been installed on).
If you decode the data (e.g. in PHP you can do: print_r( $facebook->getSignedRequest() ); to print the decoded version), you will see something like:
Array
(
...
[page] => Array
(
[id] => 1234567890
[liked] => 1
[admin] => 1
)
[user] => Array
(
...
)
)
The $response['page']['id'] is the ID of the Page that is looking at your app at that moment. You can store the IDs of the page that have installed your app, and check it with this to determine which content to load up.
You will also notice that the data includes $response['page']['admin'], which tells you if the user looking at the page is an admin of the page (1=admin, 0=not admin). And $response['page']['liked'], which tells you if the user looking at the page is a fan (1=fan, 0=not a fan).

Facebook Graph api How to get page ID via an access token

For "users" on Facebook, I gather information like their user ID by using this graph api call:
https://graph.facebook.com/me?access_token=...
This works every time.
My question is... is there a way to get the page information about the page that was just authorized via the access token that is returned after authorization?
I tried this, and id doesn't work, but this is what I'm looking for
https://graph.facebook.com/page?access_token=...
As you know, in order to post to a "page" wall is to 'Post' like so...
https://graph.facebook.com/PAGEID/feed?message=blahblah&access_token=XXXXXXXXXXXXX
How do I get the page ID and other info about the page that was authorized without already knowing the ID or page "username" (which you have to have 25 likes to use anyway)???
Thank you for any help stackoverflow community:)
The answer I was looking for is here. You can simply use a users access_token to get a set of their page_access_tokens, and give them the option of posting to these pages using these access tokens based on the return JSON object data.
pages are not authorized, applications are.
in case that your application has permissions, the current page is informed thru the signed_request that FB passes to the app canvas. use the php sdk to read it
https://developers.facebook.com/docs/authentication/signed_request/
There's no callback when an application is installed on a page. However, if a user is using your application via a tab iFrame you can get that pages id from the signed_request. You could maintain this information as part of your own user session, and use it as you see fit.
The contents of a signed_request to a tab iFrame would be similar to the following (output of PHP print_r):
stdClass Object
(
[algorithm] => HMAC-SHA256
[issued_at] => xxxxxxxxxx
[page] => stdClass Object
(
[id] => FAN_PAGE_ID // target page id
[liked] => 1 // is the user a fan
[admin] => // is the user an admin
)
[user] => stdClass Object
(
[country] => ie
[locale] => en_GB
[age] => stdClass Object
(
[min] => 21
)
)
)
For more information see:
http://developers.facebook.com/docs/authentication/signed_request/
You may also want to take a look at the manage_pages permission
Also, see here:
http://developers.facebook.com/docs/reference/api/
Page Login
You can impersonate pages administrated by your users by requesting the manage_pages permission.
Once a user has granted your application the "manage_pages" permission, the "accounts" connection will yield an access_token property for every page administrated by the current user. These access_tokens can be used to make calls on behalf of a page. The permissions granted by a user to your application will now also be applicable to their pages.

facebook: how to show content in page only if user likes it

how can i show or display information only if the user is fan or likes my page? there are several pages that do that to catch more fans. thanks!
edit:
like the victorias secret page with this box
http://www.facebook.com/victoriassecret?v=app_117241824971601&ref=ts
My answer applies if you use an iFrame for your tab/canvas. First, you need to ask for permissions (just need basic info from user). Then, make a call to the Javascript API, like so:
FB.api('me/likes', function(response) {
});
This gives a JSON object of everything the user has liked. So, you have to loop through the response and try to find your page. If you are successful in finding it, display the awesome content!
Hope this helps!
Edit: Looking back, my answer was way off. This information is actually contained in the signed_request given by Facebook.
If you mean Facebook Page, use functions from Facebook API: Signed Request
$signed = parse_signed_request($_REQUEST['signed_request'], YOUR_APP_SECRET_KEY);
$signed = Array (
[algorithm] => HMAC-SHA256
[expires] => 13..
[issued_at] => 13..
[oauth_token] => ...
[page] => Array (
[id] => 29...
[liked] => 1
[admin] =>
)
[user] => Array ( [country] => cz [locale] => en_US [age] => Array ( [min] => 21 ) )
[user_id] => ..
)
If current user like your Facebook tab page, you find out from $signed['page']['liked']
If you're using the php sdk, you can use a call to the facebook object api and then determine if they have the like you want them to have.
$path = 'me/likes';
$likes = $facebook->api($path);
More info here
This is a privacy and security setting. Facebook Privacy FAQ
Page Tabs gets Signed Request that contain "liked" field
http://developers.facebook.com/docs/authentication/signed_request/
FQL isFan
http://developers.facebook.com/docs/reference/fql/page_fan/
Old REST API isFan
http://developers.facebook.com/docs/reference/rest/pages.isFan/
Legacy FBML isFan
http://developers.facebook.com/docs/reference/fbml/visible-to-connection/
There doesn't have to be any code involved. Just create a landing page (you could create a jpeg photo and link to the photo) and set that as the default landing page in your page settings. For users who are fans they go directly to the wall posts. All other people (non-fans) go to your specified landing page.
By the way, you realize that people can still access your content by just clicking on one of the tabs, correct? Even the Victoria Secrets example allows you to click on the "wall" tab, for example, and see the content.
A lot of people just like the page because it's the default landing page and they "think" they have to like the page first. It's more social psychology than anything else.
Google for Facewall Script, that should do what you want. But keep in mind to offer an option for users that not have Facebook Account.