In facebook connect, how can I check if a user is a fan of my facebook page? Is it possible to track? - facebook

I am trying to determine if a user is a facebook fan. I load the facebook JS library and initialize:
<script type="text/javascript" src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US"></script>
FB_RequireFeatures(["XFBML","Connect","Api"], function() { FB.init("my_api_key","xd_receiver.htm") });
FB.ensureInit(function () {
FB.ApiClient.pages_isFan(my_profile_id,"some_UID",callback);
});
However when I call the API client with FB.ApiClient.pages_isFan, I get a JS error -
FB.ApiClient is undefined
I am also using the FBML fan tag to display the "become a fan" button:
<fb:fan profile_id="my_profile_id" stream="0" connections="10" logobar="1" width="300"></fb:fan>
And would like to be notified when either the "become a fan" button is clicked or a user has successfully become a fan.
The business logic is pretty simple - If they become a fan, track it in my database. Then if they try to become a fan again, check with the library if they are a fan and say "You are already a fan" if they are a fan, show the widget if not.

Using the new JS SDK:
<fb:login-button perms="user_likes">
Grant Permissions to Allow access to Likes
</fb:login-button>
<button onclick="checkDoesLike()">Check if user Likes the Page</button>
<h1>Like this Application's Page</h1>
<fb:like-box profile-id="184484190795"></fb:like-box>
<script>
window.checkDoesLike = function() {
FB.api({ method: 'pages.isFan', page_id: '184484190795' }, function(resp) {
if (resp) {
Log.info('You like the Application.');
} else {
Log.error("You don't like the Application.");
}
});
};
</script>
A working example: http://fbrell.com/fb.api/does-like

Please note that #daaku's answer is true only in two cases:
If your app grant the user_likes permission or
The user's pages privacy setting is set to everyone.
Source:
Note: This call no longer requires a
session key. You must pass a user ID
if you don't pass a session key. If
the user's pages are set to less than
everyone privacy, you must ask the
user for the user_likes extended
permission and include a valid user
access token in the API call.
Try this code to better understand:
<button onclick="checkDoesLike()">Check if user Likes the Page (wrong way)</button>
<button onclick="checkDoesLike2()">Check if user Likes the Page (right way)</button>
<script>
window.checkDoesLike = function() {
FB.api({ method: 'pages.isFan', page_id: '184484190795', uid: '579187141' }, function(resp) {
if (resp) {
Log.info('579187141 likes the Application.');
} else {
Log.error("579187141 doesn't like the Application.");
}
});
};
window.checkDoesLike2 = function() {
FB.api({ method: 'pages.isFan', page_id: '184484190795', uid: '579187141' }, function(resp) {
if (resp == true) {
Log.info('579187141 likes the Application.');
} else if(resp.error_code) {
Log.error(resp.error_msg);
} else {
Log.error("579187141 doesn't like the Application.");
}
});
};
</script>
Working example.
I've also written a full tutorial about this today.

not sure if this is helpful at all, but I inspected the DOM with firebug and I found that it has moved onto the Facebook object
var pageID = 'your page';
var uid = FB.Facebook.apiClient.get_session().uid;
FB.Facebook.apiClient.pages_isFan(pageID, uid, function(result){ alert("boo!");});

Related

Get post-id after FB.ui() share

I need to get post-id after user clicks on the share button. My code so far:
$("#share").click(function(e) {
e.preventDefault;
FB.ui({
method: 'share',
href: 'developers.facebook.com',
}, function(response){
if(response && !response.error_code) {
console.log(response); // => []
} else {
alert('err');
}
});
});
After content is successfully shared, the response I get is an empty array with no data. How can I get the post-id of share post?
Check out the docs for information about the response:
Only available if the user is logged into your app using Facebook and has granted publish_actions. If present, this is the ID of the published Open Graph story.
Meaning, you will only get the post-/object-id if the user is authorized with the publish_actions permission.

How to check user is fan (or not) of any page with javascript in my ASP.NET site (without fb app)?

In my ASP.NET site, assume that I have facebook user_id and page_id and want to determine user like page or not. I found a samples with creating application, then check user like. I dont want that, firstly user login to application, then cheking. Can I check this without application? What should I do for this? Or other advice please.. If witout application it is not possible, so, how can I hide application login window?
I want user dont use any application, dont know I check his likes.
I need result like this: yes or no
(Sorry for my bad english)
facebook Fan gate, don't forget to include the javascript sdk in your page
FB.getLoginStatus(function (response) {
if (response.status == 'connected') {
var user_id = response.authResponse.userID;
var page_id = "00"; //page id of the page that should be liked
var fql_query = "SELECT uid FROM page_fan WHERE page_id =" + page_id + " and uid=" + user_id;
var the_query = FB.Data.query(fql_query);
the_query.wait(function (rows) {
if (rows.length == 1 && rows[0].uid == user_id) {
//user like page
}
else {
//user not like page
}
});
} else {
//user not logged in
}
});
}
Put it in a .js file and name it something like facebookscript.js and then in include it in
<head> <script src="facebookscript.js" type="text/javascript"></script> </head> like this.
Take a look at this page how to include the javascript sdk: https://developers.facebook.com/docs/reference/javascript/ otherwise this code wont work.
if the user isn't logged in or have auth... your app send them to login function like this:
function login() {
FB.login(function (response) {
if (response.authResponse) {
console.log('Access Token: ' + response.authResponse.accessToken);
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, { scope: 'email' });
}
Put the login function where I put the comment tags "//user not like page and //user not logged in" in my last comment.

User Skips Permission in Facebook Auth Dialogue Javascript SDK

I have an app in a page tab that uses the facebook javascript sdk. When a new user comes to the app, I get the expected "Log in with facebook" pop-up. I also have some extended permissions that I've put in the scope parameter of FB.Login. After the users logs in with facebook I see the expected extended permissions pop-up. The only problem is if the user skips the extended permissions, the dialogue returns back an access_token, but it's not valid for the extended permissions. Code example below.
window.fbAsyncInit = function () {
FB.Canvas.setAutoGrow();
FB.init({
appId: facebookAppId,
status: true, // check login status
});
function updateFBInfo(response) {
console.log('updateResp:');
if (response.authResponse) {
//user is already logged in and connected
FB.api('/me', function (info) {
displayUserInfo(info, response);
});
}
else {
FB.login(function (loginResponse) {
if (loginResponse.authResponse) {
FB.api('/me', function (info) {
displayUserInfo(info, loginResponse);
});
}
}, { scope: 'email,manage_pages,offline_access,publish_stream' });
}
}
FB.getLoginStatus(updateFBInfo);
};
I guess my question is either, what am I missing (this has got to be something easy), or is there a way to check and see if the returned acces_token is actually valid?
You need to check explicitly for the permission before proceeding. If they haven't provided the necessarily permissions, you need to display FB.login() with the necessary scope again.
Here is the code for checking permissions:
FB.api('/me/permissions', function (response) {
var perms = response.data[0];
// Check for publish_stream permission in this case....
if (perms.publish_stream) {
// User has permission
} else {
// User DOESN'T have permission. Perhaps ask for them again with FB.login?
}
} );

Redirect to page with add page tab dialog

Facebook recently notified they are deprecating support for app profile pages.
Apps created after Dec 10th no longer have the app page option, together with the
"add to my page" functionality, and must use the new Add page tab dialog.
After the user selects which page to add the application to, is there any way to
redirect the user to the selected page?
Similar functionality existed in the "old" add to page dialog, e.g.
https://www.facebook.com/add.php?api_key=MY_ADD_ID&pages=1
Activating the dialog with a response function seems to bring no result.
`
// Add app to page
function addToPage() {
// calling the API ...
FB.ui({
method: 'pagetab',
redirect_uri: 'MY_URL',
},function(response) {
alert(response);
});
}
`
So, two questions:
a) Is there any possibility for the app using the dialog to "know" which page was selected?
b) Is there any way to redirect the user to the selected page.
Thx!
<script type="text/javascript">
function addToPage() {
// calling the API ...
FB.ui(
{
method: 'pagetab'
},
function(response) {
if (response != null && response.tabs_added != null) {
$.each(response.tabs_added, function(pageid) {
alert(pageid);
});
}
}
);
}
</script>
Use above code...you will get page id of pages selected by the user
but what if the user has a custom name for its page.
i modify devson.. code a bit
FB.ui(
{
method: 'pagetab',
redirect_uri: '',
},
function(response) {
if (response != null && response.tabs_added != null) {
$.each(response.tabs_added, function(pageid) {
FB.api(pageid, function(response) {
alert('redirect to ' + response.link);
});
});
}
}
);
This used to be simple, using the facebook UI.("Add to My Page") Unfortunately facebook removed this.
You can add it using www.facebook.com/dialog/pagetab?app_id=YOUR_APP_ID&next=YOUR_URL
I put this an html and publisched it below. Just visit, enter your app params, hit submit, and our done.
http://www.jibecompany.com/2012/add-a-facebook-page-tab-application-to-your-page

Advice needed about Facebook's dialog feed, sharer and like buttons

I'm totally confused about Facebook's dialog feed, sharer and like buttons (I'm a noob to Facebook and its jargon)
On my website, I would like the following
to have a button at the top of the page where people can share my site to their wall
to have a button on the the page where users can share their main homepage on my site to their wall
to have a button where users can share individual items of theirs (events) to their wall
I've read the sharer button is being phased out in preference to the Like button but I see a problem with the Like button. I think something can only be liked once. A user's event might want to be shared several times (to remind their Fb users of the event)
I've seen the BBC use the dialog feed button to post items to a wall and that looks okay but I couldn't get the popup to work.
my requirements are to have different links for each of the three scenarios I've mentioned, with a different description for each scenario but sharing my site's logo.
I've read so much that I'm now totally confused plus all the APIs that Fb has. It's just crazy (or that's how it seems to me)
If someone could help clear the fog with sound recommendations and some sample code for each button, I would be very grateful.
Thank you.
.
For what your talking about you would want to use the feed dialog. It's flexible enough that you just need to setup your links and pass in some variables. I don't see the feed dialog being phased out as you mentioned they serve different purposes. I think they push the like as it's easier to use and not as likely to be abused.
You'll need to register an application and make sure to initialize the Facebook javascript SDK before you call the below function. If you're having issues with your popup it's probably due to the fact your FB init process is having issues. I've added a second function I use to init Facebook. Both functions use jQuery so you might have to modify if you don't use it.
You can create a pretty generic function like this:
$.shareMe = function(myName, myLink, myPicture, myCaption ) {
FB.ui(
{
method: 'feed',
name: myName,
link: myLink,
picture: myPicture,
caption: myCaption
},
function(response) {
if (response && response.post_id) {
alert('Thanks for Sharing.');
} else {
alert('Post was not published.');
}
}
);
}
and for Facebook initialization:
$.initFacebook = function(options){
$('#fb-root').remove();
$('body').append('<div id="fb-root"></div>');
var settings = {
'appId' : null,
'callback' : null,
'channelUrl' : null,
'status' : true,
'cookie' : true,
'xfbml' : true
};
if ( options ) {
$.extend( settings, options );
}
if( typeof( xc_app_id ) == 'undefined' ) { window.xc_app_id = settings.appId; }
window.fbAsyncInit = function() {
if(settings.channelUrl==null) {
FB.init({appId: settings.appId, status: settings.status, cookie: settings.cookie, xfbml: settings.xfbml, oauth: true, authResponse: true });
} else {
settings.channelUrl=location.protocol+'//'+settings.channelUrl;
FB.init({appId: settings.appId, status: settings.status, cookie: settings.cookie, xfbml: settings.xfbml, oauth: true, authResponse: true, channelUrl: settings.channelUrl });
}
if(typeof settings.callback == 'function'){ settings.callback.call(this); }
};
(function() {
var e = document.createElement('script');
e.async = true;
e.src = '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
}
and to call it you just use $.initFacebook({appId,'yourAppId'}); There are other options there you can lookup in the docs if you need them.