I decided to leave authResponse
caching and call FB.login()
every time. It briefly opens a pop-up window, but at least there is no case when the user went to another tab or window, and the cached authResponse
become obsolete. In short, the flashing pop-up is not that important, as I don't think users will share the same page multiple times. Anyway.
Here is my last code, which is even simpler than the original:
<button id="fb-publish">Share to Facebook</button> <script type="text/javascript"> (function() { FB.init({ appId: MY_FACEBOOK_APP_ID, cookie: true, status: true, xfbml: true, oauth: true }); var fbShare = function() { FB.ui({ method: "feed", display: "iframe", link: "http://example.com/", caption: "Example.com", description: "Here is the text I want to share.", picture: "http://example.com/image.png" }); }; $("#fb-publish").click(function() { FB.login(function(response) { if (response.authResponse) { fbShare(); } }, {scope: 'publish_stream'}); }); })(); </script>
And as Tolga Arican mentioned, if itβs good for you that the sharing dialog opens in a pop-up window, you donβt even need to call FB.login()
and ask for publish_stream
permission; just call FB.ui({ method: "feed" })
and you will go well:
<button id="fb-publish">Share to Facebook</button> <script type="text/javascript"> (function() { FB.init({ appId: MY_FACEBOOK_APP_ID, cookie: true, status: true, xfbml: true, oauth: true }); $("#fb-publish").click(function() { FB.ui({ method: "feed", link: "http://example.com/", caption: "Example.com", description: "Here is the text I want to share.", picture: "http://example.com/image.png" }); }); })(); </script>
source share