How to check if someone is liked my fanpage from my website? - facebook

I want to check if a user liked my fan-page from my website and if he did i skip the step and if he didnt i want to suggest him to like my fan-page,its all on my website i dont want to put a landing page or something else on my fan-page.all of it is on my website,i don't want to put a landing page or anything else on my fan-page.
Thanks

there are only 2 ways for this:
-) use the edge.create event: https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/
downside: this can only check right after clicking a like button on your website. so, if the user comes back, you canĀ“t be sure if the like button is still clicked. also, you never get the id of the user, of course
(deprecated)
-) authorization with facebook login > you can check for the user likes > user_likes permission needed. but i doubt that you really want to authorize the user just for this...
the graph api call you could use:
me/likes?target_id=[page-id]
subscription:
permission user_subscriptions needed. graph api call would be the following:
me/subscribedto?user=[user-id]

You need user_likes permission to view like information about the current session user.
refer to: http://developers.facebook.com/docs/reference/login/user-friend-permissions/
then simply use fql to request information.
SELECT page_id FROM page_fan WHERE uid=me() AND page_id=22934684677
If user likes the page graph will return id of page.
Example graph request like for page Facebook.
http://developers.facebook.com/tools/explorer/135669679827333/?fql=SELECT%20page_id%20FROM%20page_fan%20WHERE%20uid%3Dme()%20AND%20page_id%3D20531316728
{
"data": [
{
"page_id": 20531316728
}
]
}
Else array will be empty.
{
"data": [
]
}

Related

Facebook Graph API: Webhook for LIKE and UNLIKE plus receiving the user

I have subscribed to the "feed" event with a webhook. So far I receive the following object when someone clicks on the "like" button of my page:
{"entry": [
{"changes": [
{
"field":"feed",
"value":{
"item":"like",
"verb":"add",
"user_id":11111111111111
}
}],
"id":"2222222222222",
"time":33333333
}],
"object":"page"}
Question 1: I wounder, is it possible to also get an event when the user "unlikes" the page?
Question 2: With this user_id, how can I get the user object from facebook? Is this the "real" facebook user id (i could not get an user object through the graph explorer so far) or is a page specific id of the user?
Thanks in advance!
EDIT: For Question 2, I just needed to add the page access_token to the request to get the user object.

How to check if a Facebook user liked an FB page from an external website

I have a web page which displays Facebook LIKE Box.. I need to check if a user liked my Facebook page or not and present contents to them accordingly.. doing Facebook authentication for this from my external web page is perfectly fine..
You need to use the Graph API with and check the User's likes. Here is an example.
https://graph.facebook.com/{USER_ID}/likes/{PAGE_ID}
If the user likes the page in question, it will return JSON looking like this if the user likes the page.
{
"data": [
{
"name": "bread&cup",
"category": "Restaurant/cafe",
"id": "157758372652",
"created_time": "2012-02-15T15:36:38+0000"
}
],
"paging": {
"next": "https://graph.facebook.com/me/likes/157758372652?format=json&limit=5000&offset=5000&__after_id=157758372652"
}
}
And like this if the user doesn't like the page:
{
"data": [
]
}
Here's the documentation on User likes:
https://developers.facebook.com/docs/reference/api/user/#likes
There is another way that doesn't require permissions in advance, but doesn't work perfectly on its own - ideally use it in conjunction with a log in system.
1) Subscribe to the Facebook.edge event (this means 'do something when like is clicked')
2) Create a cookie when user likes the page
Then, when a user returns, you can run the following code:
1) If user logged in, check FB API and check properly
2) If user not logged in, check cookie
This means the user who liked your site will get full access to whatever it is you're giving to fans only from that computer until they clear their cookies - at which point they will have to sign in to your site and provide like permissions.
Here's facebook edge event - Facebook edge.create
Here's create cookie stuff - How do I set/unset cookie with jQuery?
Dave

In facebook-How can I get if the user is admin of the page using javascript?

I have created application and page in facebook .Then added application into the page and also I have assigned two more user as a admin for that particular page.
I have used the below code for getting admin of that page
FB.api({
method: 'fql.query',
query: 'SELECT page_id, type from page_admin WHERE uid='+id
},
function(response) {
if(response.length){
alert('User is an Admin');
}
else{
alert('User is not an Admin')
}
}
);
This code getting the admin of that page only for current session user(that is me),not an other two person.
but that two person also admin of that page.but this code display "user is not an admin".
I want to display two person also admin of that page.
how to display other two person also admin of that page?
Please give me some suggestion or ideas.
Thanks in advance.
maybe if you allow the manage_pages permission you'll have more luck. Here is a link to the facebook documentation detailing the Page endpoint and what properties you can get from it.
Facebook page documentation - admins.
To get the full list of admins you might have to select all of the pages members and run this test on them :
To check if a specific user is an admin of the Page, issue an HTTP GET
request with the appropriate PAGE_ID, Page access token, and USER_ID
to https://graph.facebook.com/PAGE_ID/admins/USER_ID

How to show the Extended Permission Dialog in FB using new Graph API?

I used the old rest api for showing the Permission Dialog in Facebook before.
Now, with the new graph API, what can I do? (I'm in IFrame Apps).
I know that I can cheat and popup the permission in a seperate window:
FB.login(function(response) {
if (response.session) {
if (response.perms) {
// user is logged in and granted some permissions.
// perms is a comma separated list of granted permissions
} else {
// user is logged in, but did not grant any permissions
}
} else {
// user is not logged in
}
}, {perms:'offline_access'});
like that.. call the FB.login again (let say I want people to click on a different button and trigger the extended permisison dialog)
However,it looks ugly,and it doesn't look like a dialog.
Is there a way to generate the dialog? I try to figure out whether FB.ui can help but there is only little information about that.
In addition, I don't think the 'response' callback ever execute. Neither I click "Don't allow" or "allow", won't trigger any call back. any idea?
hihih..anyone can help me?
Finally. find out the solution from another website.
first. after FB.init( ... ); do that:
FB.provide("UIServer.Methods",
{ 'permissions.request' : { size : {width: 575, height: 300},
url: 'connect/uiserver.php',
transform : FB.UIServer.genericTransform }
} );
Then, whenever you need to call the permssion dialog, do that:
FB.ui({method: "permissions.request", "perms": 'email,offline_access'},
callBack);
It took me so long to figure out by looking at the FB object and find out there is UIServer with permissions.request then from that, I keep searching and find this solution. and FB.ui talks nothing about it.. and FB.provide is not documented. THANKS facebook.
You don't need to use javascript or any SDK for this although it would make it easier. You need only to redirect the user to a url like this:
https://graph.facebook.com/oauth/authorize?
client_id=...&
redirect_uri=http://www.example.com/callback&
scope=user_photos,user_videos,publish_stream
You should always redirect the user to the top window either with javascript or the link.
window.top.location = <login_url> or Login
If you are using the PHP SDK or the C# SDK you could have the sdk generate the url for you, but the process is the same.
Also, not that the redirect_uri has to be on the same domain as your iFrame application's url. This will cause Facebook to redirect your user outside of Facebook to your website, you then should redirect the user back to the app inside of facebook. For example:
User clicks login
user goes to Facebook login page
User clicks allow
Facebook redirects the user to http://www.example.com/callback
Your app redirects the user to http://apps.facebook.com/myapp/loggedin
One of the answers suggests a hack in which you call FB.provide() and then FB.ui() to pop up the extended permissions dialog. That solution doesn't work for me, but there is a documented solution now that does. Just call FB.login() to get the permissions you need.
FB.login(function(response){
if (response.authResponse) {
alert('success!');
} else {
alert('fail!');
}
},{scope: 'email'});
Even better, you can ask for extended permissions with the login button.

Facebook Graph API + Facebook Pages

Using Facebook's Graph API, given a username xyz (assuming they've authenticated my site), how do I get a list of all of the facebook pages that the user administers?
The accounts property on the user object says:
The Facebook pages owned by the current user. If the manage_pages permission has been granted, this connection also yields access_tokens that can be used to query the Graph API on behalf of the page.
http://developers.facebook.com/docs/reference/api/user
After getting access token you can get all details of list of all of the facebook pages that the user administers.
https://graph.facebook.com/[FACEBOOKUSERID]?metadata=1&access_token=
The output will look like
{
"name": "Facebook Developer Garage Austin - SXSW Edition",
"metadata": {
"connections": {
"feed": "http://graph.facebook.com/331218348435/feed",
"picture": "https://graph.facebook.com/331218348435/picture",
"invited": "https://graph.facebook.com/331218348435/invited",
"attending": "https://graph.facebook.com/331218348435/attending",
"maybe": "https://graph.facebook.com/331218348435/maybe",
"noreply": "https://graph.facebook.com/331218348435/noreply",
"declined": "https://graph.facebook.com/331218348435/declined"
}
}
}
Use an fql query! That is the best way to get the pages for which the user is admin. You will also be able to restrict the names also. i.e pages with empty names A sample fql query which gives you the page details as well.
SELECT page_id,page_url,name,pic_square FROM page WHERE page_id IN (SELECT page_id FROM page_admin WHERE uid = " + **UserId** + ") and name!=''
UserId -- Id of the admin
NOTE
This method will not work from version 2.1 of graph api as fql was deprecated from that version onwards
I found the answer, you need to use FQL, passing in the appropriate access_token:
https://api.facebook.com/method/fql.query?query=SELECT%20page_id%20FROM%20page_admin%20WHERE%20uid=XXXX&access_token=YYYY
Here's what I use. It works perfect
$pages = $facebook->api(array('method' => 'fql.query','query' => 'SELECT page_id FROM page_admin WHERE uid = '.$uid.''));
foreach($pages as $k=>$v) {
echo 'page id#:'.$v['page_id'].'<br/>';
}
This is of course after you have the session created for the fb user! The $uid would be the profile id# of the specific facebook user your returning of a list of managed pages for.
#rmorrison - This is not graph API though. With the new "like" addition, you can use this URL: h ttps://graph.facebook.com/me/likes?access_token=blah! or h ttps://graph.facebook.com/USER_ID/likes?access_token=blah!
Fql is the best way to get the pages of the user for which he is the admin. As the others like likes of user will give all the pages that the user like.
You can get list of pages from a simple Facebook Graph API Hit.
https://graph.facebook.com/{user-id}/accounts?access_token={access-token}
It will give a list of pages for user which he/she has created.
access_token is valid for an hour or less. What should someone do in order to get the details even after. I was trying to result get the result from
https://graph.facebook.com/v2.3/search?q=coffee&type=place&center=37.76,-122.427&distance=1000&access_token=xyz
This worked for an hour or so but how do i get the result after an hour...without going for a login.