使用Javascript将动态内容分享到Facebook新闻Feed的最简单方法?

时间:2011-11-28 07:06:17

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

我希望我的用户能够在他们的Facebook新闻Feed上分享动态内容。没有其他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做到这一点?或者我可以吗?

2 个答案:

答案 0 :(得分:1)

我决定省略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>

答案 1 :(得分:0)

1)确保appId已定义。

2)在FB.login中,response.authResponse可能无效。我不太确定但只是抛出console.log,它可能是response.session

3)在方法:feed中,你可以为图片放置更大的图像,但它会缩小尺寸。无法将大图像作为Feed缩略图。它只是一个饲料的拇指..

对于最轻的方法:喂js,你不需要显示:iframe我猜。

FB.ui( {
method: 'feed',
name: 'Facebook Dialogs',
link: 'http://developers.facebook.com/docs/reference/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.' }, function(response) {} });