Facebook 的帖子“签名请求”中缺少 CSRF 令牌;

发布于 2024-10-30 20:37:10 字数 552 浏览 0 评论 0原文

我正在处理 Django 项目。目的是从 Facebook 导入用户信息。首先,我使用 Facebook 提供的注册社交插件。我有一个基本模板,其中包括用于注册插件的 iframe;就像 Facebook api 文档建议的那样。呈现此模板的视图如下:

def registration(request):
    if (request.method == "POST"): 
        return HttpResponse("it posted!")
    else: 
        return render_to_response("ui/registration.html", {}, 
                                  context_instance=RequestContext(request))

当我在插件上按下注册并且 Facebook 向我的视图发送签名请求时,Django 就会抱怨缺少 csrf 令牌。我还尝试通过使用 csrf(request) 在上下文字典中传递 csrf-token 来显式包含 csrf-token,但这仍然不能解决问题。

I am working with a Django project. The aim is to import user information from Facebook. For a start, I am using the registration social plugin that Facebook offers. I have a basic template that includes the iframe for the registration plug-in; just the way the Facebook api documentation suggests. The view that renders this template is as follows:

def registration(request):
    if (request.method == "POST"): 
        return HttpResponse("it posted!")
    else: 
        return render_to_response("ui/registration.html", {}, 
                                  context_instance=RequestContext(request))

As soon as I press register on the plugin and Facebook sends my view the signed-request, Django complains about the missing csrf token. I also have tried explicitly including the csrf-token by passing it along in the context dictionary using csrf(request), however that still doesn't solve the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

━╋う一瞬間旳綻放 2024-11-06 20:37:10

CSRF 保护是为了防止跨站发帖。但是,在这种情况下,您希望接受来自 Facebook 的帖子,因此您应该在接受签名请求的视图上使用 csrf_exempt 装饰器。请参阅有关 CSRF 异常的部分: http://docs.djangoproject.com /en/1.3/ref/contrib/csrf/#exceptions

CSRF protection is there to prevent cross-site posts. However, in this case you want to accept the post from Facebook so you should use the csrf_exempt decorator on your view which accepts the signed request. See the section on CSRF Exceptions: http://docs.djangoproject.com/en/1.3/ref/contrib/csrf/#exceptions

如梦初醒的夏天 2024-11-06 20:37:10

为此,您需要做一些事情:

  1. 您需要将您的 csrf 令牌作为参数传递给您的 facebook 请求,如其 文档
  2. 接下来,您必须使用 @csrf_exempt 来装饰您的视图,就像 Mark 建议的那样。
  3. 最后,在您看来,您可以验证csrf令牌是否没问题。您可以通过查看 django csrf 中间件代码来窃取一些逻辑 这里

或者,您可以编写自己的中间件来检查来自 facebook 的 csrf 令牌,而不是执行步骤 2 和 3。

You need to do a few things for this:

  1. You'll need to pass your csrf token as a parameter to your facebook request as seen at the end of their documentation:
  2. Next, you'll have to decorate your view with @csrf_exempt, like Mark suggested.
  3. Finally, in your view, you can verify that the csrf token is okay. You can steal some logic by taking a look at the django csrf middleware code found here.

Alternately, instead of steps 2 and 3 you can write your own middleware to check the csrf tokens coming from facebook.

歌枕肩 2024-11-06 20:37:10

寻找 fandjango 应用程序,尤其是中间件。
https://github.com/jgorset/fandjango
这对我造成了

look for fandjango app, especially the middleware.
https://github.com/jgorset/fandjango
that did it to me

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文