2010-11-21 69 views
0

在我的django网站中,我使用新的facebook javascript SDK来允许我的用户向朋友发送朋友邀请。使用新的JavaScript SDK发送Facebook好友邀请显示403禁止页面,但邀请发送成功

但是当用户发送的网站邀请(登录&从Facebook的弹出选择朋友后),邀请是发送成功,但用户会看到一个“403禁止 - 跨站请求伪造检测请求中止”页(在发送邀请的网址相同)。如何克服这种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保护的表单。

回答

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 
      }); 

工作非常适合我。

hf

+0

不错。我投票接受这个答案。 – 2012-01-10 15:52:28