Don't display facebook like button for those w

2019-03-30 20:49发布

问题:

I want to display a Facebook like button for a page only for those who didn't like that page. So, when a user is already a fan of the page , the like button will not be shown.

I did a lot of research and I found that I need to implement Facebook connect, and fetch the user_likes permission and check if the connected user has the page in his list. This is a working example http://www.fbrell.com/fb.api/does-like

Basicaly, I need the code that will do this :

When the user clicks on connect, the content will be changed and the Facebook like button will be shown if the connected user didn't like it in the past.

回答1:

Implement the JavaScript SDK

During the initialization of the JS SDK, make sure to set xfbml to false, so that the plugin will not be rendered automatically. You'll have to request the user_likes permission which means that you are going to have to associate your site with a Facebook application. This is so that Facebook can keep track of who is requesting what data. You can use the Fb.login() method to request permissions and authenticate your users.

Once you have the JavaSctipt SDK you can simply make a call to the API via

  • Graph
    FB.api('me/likes/PAGE_ID')

  • FQL
    FB.api("fql?q='SELECT+uid+FROM+page_fan+WHERE+uid=me()+AND+page_id=PAGE_ID")

If those return data you know not to display the like plugin. However, if the user has not liked the page yet, those calls will return empty results and then you can call the FB.XFBML.parse() method to render the like plugin.



回答2:

You need to make some calls to the Graph API,
After you get the Page ID with FB.api('http://your-url.com');
you can check if current user like's your page with an FQL query:

'SELECT uid FROM page_fan WHERE uid=me() AND page_id=YOUR-PAGE-ID'


回答3:

If your like button is on a canvas page or a page tab you can implement a fan gate by reading the signed_request. There is a ton of tutorials and hints on how to do it also here on stackoverflow for different server side languages. For example this answer here: https://stackoverflow.com/a/7671496/601466
Just search for "fan gate" and "signed request".