javascript - 使用 Javascript 将动态内容共享到 Facebook 新闻提要的最简单方法?

标签 javascript facebook facebook-graph-api share facebook-like

我希望我的用户能够在他们的 Facebook 动态消息中分享动态内容。没有其他 Facebook 集成(例如 Facebook 登录或其他服务器端集成),所以我希望它尽可能简单。

这是我得到的地方,但它看起来如何?这似乎可行,但我不确定我是否已考虑到所有内容。

<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 fbAuth = null;
    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() {
        if (!fbAuth) {
            FB.login(function(response) {
                if (response.authResponse) {
                    fbAuth = response.authResponse;
                    fbShare();
                }
            }, {scope: 'publish_stream'});
        } else {
            fbShare();
        }
    });
})();
</script>

此外,如果我想从 URL 附加图像(比 picture 字段中定义的 50x50 像素图像大),我如何仅使用 JavaScript 来实现?或者我可以吗?

最佳答案

我决定省略 authResponse 缓存并每次都调用 FB.login()。它会短暂打开一个弹出窗口,但至少不会出现用户在另一个选项卡或窗口中注销并且缓存的 authResponse 过时的情况。短暂闪烁的弹出窗口不是什么大问题,因为我不认为用户会多次共享同一个页面。

这是我的最终代码,它比原来的代码更简单:

<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>

正如 Tolga Arican 所提到的,如果共享对话框在弹出窗口中打开对您来说没问题,您甚至不需要调用 FB.login() 并请求 publish_stream 权限;只需直接调用 FB.ui({ method: "feed"}) 就可以了:

<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>

关于javascript - 使用 Javascript 将动态内容共享到 Facebook 新闻提要的最简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8292480/

相关文章:

Facebook API : Instagram Account Is Missing

iOS 6- : Facebook API

JavaScript 继承 : when constructor has arguments

JavaScript 正则表达式规则正在破坏 ASP.NET 缩小

android - 从 SD 卡上传图片到 Facebook - Android

facebook - 如何设置 Metatag fb :pages correctly to enable link editing for one page with multiple channels?

facebook-graph-api - Facebook 图形 API m.mid 与 m.id

javascript - 检查显示是否支持 JavaScript 中的 HDR(高动态范围)颜色?

javascript - Safari 浏览器 Javascript 数组似乎对字符串排序不正确

facebook - include_headers 应该这样放置吗?