【问题标题】:sending facebook friend invitation using new javascript SDK shows 403 forbidden page but the invitation is sent successfully使用新的 javascript SDK 发送 facebook 好友邀请显示 403 禁止页面,但邀请已成功发送
【发布时间】:2010-11-21 13:09:02
【问题描述】:

在我的 django 网站中,我正在使用新的 facebook javascript SDK 来允许我的用户向他们的 facebook 好友发送好友邀请。

但是当用户发送网站邀请时(登录并从 facebook 弹出窗口中选择朋友后),邀请发送成功,但用户看到“403 Forbidden - Cross Site Request Forgery detected. Request aborted” 页面(在发送邀请的相同 url)。如何克服这种 csrf 验证。

邀请的javascript代码(加载facebook SDK后):

<script>
function invitePopup() {
FB.login(function(response) {
    if (response.session) {
    // user successfully logged in 
    FB.ui({
        method:'fbml.dialog', 
        fbml: (
            '<fb:request-form action="http://{{site.domain}}{% url account_view %}" method="post" invite="true" type="{{ site.name }}" ' +
                'content="help the world by spreading good ideas. Join the move! <fb:req-choice url=\'http://{{site.domain}}{% url facebook_login %}?facebook_invitation=1\' label=\'Accept\' />" >' +                
                '<fb:multi-friend-selector showborder="false" bypass="cancel" actiontext="Invite your friends to join {{ site.name }}" /> '+
            '</fb:request-form>'
            ),
            size: { width:640, height:480}, width:640, height:480
        });

    $(".FB_UI_Dialog").css('width', $(window).width()*0.8); // 80% of window width
    } else {
            // user cancelled login
        }
    });    

}
</script>

以及触发部分:

<a href="#" onclick="invitePopup();" class="facebook">Invite your Facebook friends to join {{ site.name }} </a>

我尝试过一种解决方法,即为视图使用 csrf_exempt 装饰器。但我不想使用它,因为我在该视图中使用了更多需要 csrf 保护的表单。

【问题讨论】:

    标签: django facebook


    【解决方案1】:

    您可以像这样包含 crsf_token:

     FB.ui({
            method:'fbml.dialog', 
                fbml: (
                    '<fb:request-form action="http://{{site.domain}}{% url account_view %}" method="post" invite="true" type="{{ site.name }}" ' +
    'content="help the world by spreading good ideas. Join the move! <fb:req-choice url=\'http://{{site.domain}}{% url facebook_login %}?facebook_invitation=1\' label=\'Accept\' />" >'
    
     + "{% csrf_token %}"+  
    
    
    '<fb:multi-friend-selector showborder="false" bypass="cancel" actiontext="Invite your friends to join {{ site.name }}" /> '+
                    '</fb:request-form>'
                    ),
                    size: { width:640, height:480}, width:640, height:480
                });
    

    非常适合我。

    高频

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      • 2012-12-02
      相关资源
      最近更新 更多