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

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.

Related

Javascript SDK: Check if a user liked a facebook page

I want to check if a user has liked a page or not and based on it display the contents of the website!
Following is the code using FQL. Is there any way for the same using Javascript SDK?
$(document).ready(function(){
FB.login(function(response) {
if (response.session) {
var user_id = response.session.uid;
var page_id = "1553725881514063"; //Swoopshows page id
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) {
alert("Liked");
//here you could also do some ajax and get the content for a "liker" instead of simply showing a hidden div in the page.
} else {
alert("not liked");
//and here you could get the content for a non liker in ajax...
}
});
} else {
// user is not logged in
alert("Please Login to continue");
}
});
});
Also, in my SDK initiation, i have mentioned the version to be 2.0. Still this doesn't work. Any help would be appreciated.
This doesn't work anymore, because Facebook is discouraging "Like-gating" content.
See
https://developers.facebook.com/docs/apps/changelog#v2_1_90_day_deprecations
The 'liked' property will no longer be returned in the 'signed_request' object for Page Tab apps created after today. From November 5, 2014 onwards, the 'liked' property will always return 'true' regardless of whether or not the person has liked the page.
https://developers.facebook.com/docs/facebook-login/permissions/v2.1#reference-user_likes
As you'd need the user_likes permission to determine whether a user has likes the page, and thereby need an app review, you'll very likely not be able to get the permission approved:
Not allowed: Gate access to your app, or some content within your app based on whether or not someone has liked a page.

How to make user make a like action in facebook ? what is the OBJECT_ID to add to the requestWithGraphPath?

In my app there is a label which once clicked should make the user making a 'like' action on facebook.
In this question there is the following piece of code supposed to make that action :
// Change OBJECT_ID to an actual object id
Titanium.Facebook.requestWithGraphPath('OBJECT_ID/likes', {}, 'POST', function(e) {
if (e.success) {
alert('Success! From FB: ' + e.result);
} else {
if (e.error) {
alert(e.error);
} else {
alert('Unkown result');
}
}
});
i have generated an url for the page to be liked from this page but i don't think this is the required 'OBJECT_ID' for that graph request.
What is the OBJECT_ID ?!

Is there anyway that I can ask user to authenticate a specified account

We let users/companies add their facebook accounts to our system, and we let them authenticate those accounts after they are added, so that we can get details which are not public otherwise. so we need tell user which account to authenticate against, is this possible ?
Lets say user is already logged into facebook with account x, then he logins to our app and choose to authenticate account Y, so he will click on the authenticate link for Y and proceed to authenticate, is there anyway to enfore user to authenticate Y.
For me, the solution was, first check if user is already logged into facebook, and logout them using FB.logout. Then open the login dialog (FB.login) so that they can log into the desired account. On successful login, check that the logged in user is same as the link that was clicked on.
The overall code is some thing like this
$('.fb-login').live('click', function() {
var accountName = $(this).attr('id');
//if user is already logged in, logout him so that he can authenticate correct account
FB.getLoginStatus(function(response) {
console.log(response);
if (response.status === 'connected') {
FB.logout(function(resp) {
console.log("Logged out of facebook")
});
}
}, true);
FB.login(function(response) {
if (response.authResponse) {
var accessToken = response.authResponse.accessToken;
console.log(response);
console.log('Fetching FB account information.... ');
FB.api('/me', function(response) {
console.log('Account name, ' + response.username + '.');
//validate that user authenticated the same account as the link he clicked on
if(accountName == response.username || accountName == response.id) {
//save token
} else {
//show error
}
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, {scope: 'perms'});
});
Yes, you will want to have logout functionality on your page. Since it's unclear as to how you've implemented your login button, I'll give one example:
Do a server-side 302 redirect the user to https://www.facebook.com/logout.php?next=YOUR_URL&access_token=ACCESS_TOKEN
The next= will be the location where the user can click to log into account Y
For your specific needs you can research here: https://developers.facebook.com/docs/authentication/

FB undefined while using getLoginStatus in page show

I am getting FB undefined , when i write following code , i have successfully added the necessary files for facebook using window.fbAsyncInit , the thing is if i write following code under "click" event then it works well but not at the time of pageshow in jquery mobile. this is happening might be because the facebook js files are loading bit late. but i want to check the user status on every page to restrict unnecessary or anonymous access.
$("#details").live( "pageshow", function() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and connected to your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
console.log("INSIDE ME");
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
//but not connected to the app
console.log("OUTSIDE ME");
} else {
// the user isn't even logged in to Facebook.
}
});
});
Put the jQuery live() assignment inside the window.fbAsyncInit as well. So that way it is only applied to the DOM element after FB has a chance to initialize.

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

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!");});