Display something for people who like or not like our facebook page - facebook

I search a solution for the problem highlighted in this question.
Unfortunately, the accepted solution (which dates back to 21/11/2012) doesn't work anymore, as you can this in this demo.
Does someone know why?
Body
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId : '179378788777832',
status : true,
cookie : true,
xfbml : true
});
</script>
<div id="login">
You are not logged in to FB, Please click here to login.
</div>
<div id="container_notlike">
YOU DONT LIKE
</div>
<div id="container_like">
YOU LIKE
</div>
JS
var hideLogin = function(){
$("#login").hide();
}
var showLogin = function(){
$("#login").show();
}
var doLogin = function(){
FB.login(function(response) {
if (response.session) {
hideLogin();
checkLike(response.session.uid)
} else {
// user is not logged in
}
});
}
var checkLike = function(user_id){
var page_id = "40796308305"; //coca cola
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) {
$("#container_like").show();
//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 {
$("#container_notlike").show();
//and here you could get the content for a non liker in ajax...
}
});
}
$(document).ready(function(){
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
hideLogin();
checkLike(response.authResponse.userID)
} else {
showLogin();
}
});
$("#login a").click(doLogin);
});
CSS
body {
width:520px;
margin:0; padding:0; border:0;
font-family: verdana;
background:url(repeat.png) repeat;
margin-bottom:10px;
}
p, h1 {width:450px; margin-left:50px; color:#FFF;}
p {font-size:11px;}
#container_notlike, #container_like, #login {
display:none
}
I search solution for hours but I didn't find anything what works.
Thank you for help.

Like Gating is not allowed anymore, that´s why it is not possible. The only reliable way to get that information is by authorizing a user with the user_likes permission and using /me/likes/[page-id]. But you will not get that permission approved for like gating in the Login Review.
People need to like something because they really want to, not because they get something for it:
Only incentivize a person to log into your app, enter a promotion on your app’s Page, or check-in at a place. Don’t incentivize other actions
Source: https://developers.facebook.com/policy/
Btw, you can also subscribe to the edge.create event to find out if a user just clicked your like button, but you can´t find out if the user liked it before: https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/

The problem on that code is that FQL is deprecated .
You can't do what you want to, and thats why changes are necessary .
Your code would work if your app is old, created before changes that turns like gating not allowed, but anyway, you cannot use that for show content. You can use that kind of implementation for creating an interactive experience, where you can for example changing the content, saying "Thanks for liking".. Or "Connect with us, liking our page.." ..
You can also think about interfaces, where you show up the page plugin, and just after user likes, you say Thank you ... and hide the page plugin ... But user must always be able to close without liking .
For checking if user likes a page, you need use :
FB.api get on '/me/likes', and with the response ...
if (response.data[likes].name == "Coca-Cola")
or... better
if (response.data[likes].id == "40796308305") {
}
I repeat, One thing has nothing to do with another ..
You can check if user likes a page, but you cannot restrict content, based on this kind of resource .
There are other ways to check it, for exaple :
Get api call to userid/likes/pageid returns page info if user likes the page, and returns nothing if user does not like the page .
You will waste time trying do that for controlling content consumption .
Your app must be aprooved for asking user_likes permission, and its better you think about creating another experience for users, instead of submitting something like that .
I also think that content with good open graph for sharing, commenting and optional liking is very much more efetive, because i noticed that many people used to like and dislike the page after getting the content .
If you just... Prompt a FB.UI for sharing after 1 minute, for example, you will have much more results .. Aways positioning the page plugin in strategic places, people will naturally like your page ..
Than you can say change the page plugin element :
Thank you for liking, please share with your friends ....
Who would also like ...
Or use a callback for triggering the share dialog ..

OLD SCHOOL API CALL
The method FB.Event.subscribe() allowed apps to subscribe to a range of events, and define callback functions for when they fire, is deprecated .
Also FQL Query is deprecated .
For checking if user likes a page, you need user.likes permission, so you can try :
FB.api get on '/me/likes', and with the response ...
if (response.data[likes].name == "Coca-Cola")
or... better
if (response.data[likes].id == "40796308305") { }
There are other ways to check it, for exaple :
Get api call to userid/likes/pageid returns page info if user likes the page, and returns nothing if user does not like the page .
You can check if user likes a page using this call, but you cannot restrict content, based on this kind of resource .
2018 UPDATED SOLUTION
But nowadays, in 2018 the best method for is setting Webhooks .
Webhooks are a subscription based system between Facebook and your server. Your app subscribes to receive updates from Facebook via a specified HTTPS endpoint .
This allows your to app to receive notifications whenever there are updates to a chosen set of topics and their fields, so, you can track changes to most sections of the user's profile, such as About, Photos, Posts, Friends, and Likes.
Webhooks update notifications are sent as POST requests to a callback URL that you supply. Notifications can be lightweight, indicating only that a field has been updated, or can include the newly updated value .
webhooks user reference
Full list of user profile fields that you can subscribe to, such as About, Photos, Posts, Friends, and Likes.
webhooks page reference
The easiest way to set up your app to receive Webhooks updates is to use the App Dashboard's, check out Facebook Platform documentation for more info .
Webhooks documentation

Related

Why do I get this warning message on my facebook login page?

I'm trying the Facebook login API for the first time on a web app deployed locally.
I init the API with the snippet in their documentation and have a button on my HTML page
<button type="button" onclick="fbLogin()">
Where fbLogin() is
function fbLogin() {
FB.login(function(response) {
console.log(response);
// handle the response
if (response.status === 'connected') {
// Logged into your app and Facebook.
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
} else {
// The person is not logged into Facebook, so we're not sure if
// they are logged into this app or not.
}
}, {scope: 'public_profile,email,user_friends'}); // TODO: If the user doens't provide the email address prompt it for email. Also, don't ask for friend list when its a simple logout maybe? Specify why it's needed first. If it doesn't provide the friends access, re-prompt the request, explaining him it can have a more social experience if he accepts (can see when its friends win and congratulate with them, (we can also prom him to invite some friends))
}
When I try to log in the message
"You are using a display type of 'popup' in a large browser window or tab. For a better user experience, show this dialog with our JavaScript SDK without specifying an explicit display type. The SDK will choose the best display type for each environment. Alternatively, set height and width on your window.open() call to properly size this dialog if you have special requirements precluding you from using the SDK. This message is only visible to developers of your application."
shows up.
Screenshot
As you can see though, I am not specifying any specific display type; so I really don't know what to do with this message.
Other Google entries didn't have a solution for this problem.
What action do I need to take in order to remove it?

How to get ids of facebook invited friends by user in my asp.net4.0 web application

I am developing an ASP.NET application. I implemented Facebook JavaScript API in my application, for connect with Facebook and get FB friend list of user. I had done this successfully.
Now, I want to count how many friends are invited by user. Is it possible with facebook API. User can select multiple friends and i want to count how many friends selected and invited by user, Invited friends Id is bonus if We can get.
Please don't forget that facebook changed its Oauth settings for security reasons.
I'm also trying to get ids values after Send Invitation button clicked and page post back to
if (Request.Form["ids"] != null)
{
span1.InnerHtml = "ids";
//put success code here..
}
else
{
span1.InnerHtml = "oops no id";
}
if (Page.PreviousPage != null)
{
span1.InnerHtml = "ids";
//put success code here..
}
else
{
span1.InnerHtml = "oops no id";
}
but in this case program control always goes to else condition it means after page is post back form return null value.
Is there any way to get ids of invited friends.
Thanks.
If you using Facebook Requests to send invites (you really should, it's intended for this), the only way to get invited friends is via Facebook Requests Dialog callback:
FB.ui({method: 'apprequests', message: 'Whoa!'}, callback);
function callback(response){
// response.to now contain array of invited users ids
console.log('Invited friends ids', response.to);
if (response.request) {
console.log('Efficient Request id', response.request);
} else {
console.log('Requests Ids', response.request_ids);
}
}
There is no way to get request sent by user via Graph API or FQL, you only can get requests received by user. You may save all requests sent by your users and rely on this data if you need aggregated count of invitation sent...
Ok finally I resolve this issue and get invited friends ids..
As we know we use javascript sdk for FB connect so my solution is in concern of Java script:
Just make on change in your .js file where you put your facebook connect javascript code. Use Get or Request Method instead of Post Method in fb:request-form tag.
<script type="text/javascript>
var fbhtml = "<fb:serverFbml width=\"" + width_of_invitation_div + "\">
<script type=\"text/fbml\"><fb:fbml><fb:request-form action=" + window.location + " method="REQUEST" invite="true" type="" + type_of_fb_request_form + "" hold=" /><br mode="></fb:request-form></fb:fbml></script></fb:serverfbml>"
</script>
After invitation send to users selected friends you get ids[] in url.
you can get these ids through query string and split ids by comma(,) and store in array.
Hope this will help for other devs.

Automatic Timeline Action posting

Following Facebook's Guide for the new open graph, I have managed to implement Timeline and "Read Article" Action on our WordPress-based site. But the guide makes you click a button to post the action. It might be silly question, but how do we make it automatically posting once user enters the article? Is that something that's happening AFTER the action has been approved by Facebook?
Does recommendation bar do that? Is that its purpose?
Yes, the Recommendations Bar does this. You could implement something similar in your own code but the Recommendations Bar supports a bunch of things out of the box like sending the activity only once the user scrolls X% of the way down and/or after a specified time interval.
Using this markup the action should be tracked in 5 seconds after the bar has loaded:
<div class="fb-recommendations-bar" data-href="{YOUR URL}" data-read-time="5"></div>
The Recommendation Bar is still in beta and won't work for anyone other than the App Administrators / Developers / Testers.
End-users will only get actions published once Facebook has approved the action. This usually takes a day or two if you provide adequate instructions. You will need to make sure users are able to remove their activity from within your site - this is a requirement for submitting a action:
Please make sure your users can
Turn sharing on/off globally on each page an article appears.
Remove articles they shared within your app on each page an article appears.
Only generate read actions when you're sure someone is interested in reading the article.
For my WordPress site, I used some code like:
setTimeout( function() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// publish action to facebook
FB.api( '/me/{$action}', 'post', { article : '{$permalink}' } );
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
}, 2000 );
Use some JavaScript to post the action after the page loads:
window.onload=postCook;

Is authentication required for Facebook Apps?

I don't use Facebook, so at a bit of a loss here, as the API documentation doesn't quite answer this, perhaps someone here knows...
Looking at building a Facebook app to assist publishing from a content-driven web app. Obviously we need authorisation via OAuth to publish an app link to a facebook user's wall, but is authorization for everyone viewing the app within Facebook required? We have no interest in making use of the viewer's facebook data, we just want to show them a page.
If they have to authorise the app, then that's a bit of a barrier - but it's not clear if they can just view the app without anything getting in the way.
"is authorization for everyone viewing the app within Facebook required?"
If you mean having your app post a link to their wall, then yes, you will need to ask for their permission to do so. If you mean can other friends can see the post made by your app to an authorized user's feed? No the friends do not need to authorize your app to do that.
But remember Facebook now has the ability for you to specify which groups can see the post. See https://developers.facebook.com/docs/reference/api/user/#posts and read about the privacy field.
Yes, you can show app without any authentication. If you want user to share content then you can do it like this. Code is few months old, so I don't know if that works.
You cannot set custom text/message for the user. This will display popup window where user must confirm sharing/posting to wall.
<div id="fb-root"></div>
<script src='http://connect.facebook.net/en_US/all.js'></script>
<script type="text/javascript">
FB.init({appId: 'APP_ID', status: true, cookie: true});
function share(object) {
var obj = {
method: 'feed',
link: 'http://apps.facebook.com/your_app_url/',
picture: object.picture,
name: object.name,
caption: object.caption,
description: object.description,
action_links: [
{ text: 'My Cool App', href: 'http://apps.facebook.com/your_app_url/' }
]
};
function callback(response) {
}
FB.ui(obj, callback);
}
</script>
If you're doing a canvas app, you don't need to. You get this generic information in the initial HTTP post:
user A JSON array containing the locale string, country string and the age object (containing the min and max numbers of the age range) for the current user.
algorithm A JSON string containing the mechanism used to sign the request.
issued_at A JSON number containing the Unix timestamp when the request was signed.
You get no actual information about the user, though. You need to authenticate to get that.

Facebook fan page tab and user id

As per the documentation in the following link, we can get the user id if the uer will interact with the form..
http://wiki.developers.facebook.com/ind … d_Policies
"If a viewing user interacts with the tab (like submits a form, takes an action that causes an AJAX load of new content, or follows a relative URL that loads on the tab), that user's UID is sent to the application as the fb_sig_user parameter, the profile owner's user ID is sent as the fb_sig_profile_user parameter. The viewing user's session key is key is sent only if the user authorized the application. "
In my fan page tab am I have an AJAX form which the user can submit with some value.. now I need the users id also.. how can I get this..
I tried to get the value in my AJAX submit page using $_POST['fb_sig_user'] with no success.. can anyone help me with this please..
You won't be able to get the id of the user using $_POST['fb_sig_user'] unless you authenticate the user by having this in the facebook's ajax function:
ajax.requireLogin = true;
For example, I'm retrieving it fine with this:
function do_ajax(url, div_id)
{
document.getElementById('poller_waitMessage').setStyle('display', 'block');
var ajax = new Ajax();
ajax.responseType = Ajax.FBML;
ajax.onerror = function(error)
{
new Dialog().showMessage("Error:", "Some communication error occured, Please reload the page.","Ok");
};
ajax.ondone = function(data)
{
document.getElementById('poller_waitMessage').setStyle('display', 'none');
document.getElementById(div_id).setInnerFBML(data);
}
ajax.requireLogin = true; // <----- this is important
ajax.post(url);
}
I've been happily using the form variable fb_sig_profile_user for previous apps, and when developing a new app last week, the variable was no where to be found.
Searched for several days, I was about to give up, and then the found answer:
ajax.requireLogin = true;
I understand FB cares about privacy and all, but they really need to announce these kinds of changes before just taking it away.
Million Thanks!