2

我正在使用安装在 pythonanywhere 上的 django_facebook 库。当我单击从一个视图到另一个视图的链接时,我收到一条错误消息:

400 错误请求

缺少签名请求。

有人可以简要介绍一下链接在 Facebook 应用程序中的工作原理吗?

删除 @canvas_only 装饰器并不能解决问题,因为我需要访问图形 api。

这是代码:

视图.py:

from django.shortcuts import render
from django_facebook.decorators import canvas_only
#from django_facebook.decorators import facebook_required
#from django.utils.decorators import method_decorator

from models import Poem, Share
from django import forms
from django.views.generic import View

class PoemEntryForm(forms.Form):
    words = forms.CharField( widget=forms.widgets.Textarea(), initial='ENTER\nPOEM\nHERE\nONE\nWORD\nPER\nLINE' )

@canvas_only
def home(request):
    me = request.facebook.graph.get_object('me')
    my_username = me['username']
    request.session['username'] = my_username

    try:
        poems = Poem.objects.filter(user=my_username)
    except Poem.DoesNotExist:
        poems = []

    # convert poems into tuples of information relevant to the home page
    # sort them in reverse chronological order
    # ie: title and created
    poems = [(poem.title(), poem.created) for poem in sorted(poems, key=lambda poem: poem.created, reverse=True)]

    try:
        shared = Share.objects.filter(shared_to=my_username)
    except Share.DoesNotExist:
        shared = []

    shared = [(poem.title(), poem.user, poem.created) for poem in sorted(shared, key=lambda poem: poem.created, reverse=True)]

    return render(request, 'home.html', {
        'me': me,
        'my_poems': poems,
        'shared': shared,
    })

class Create(View):
    #@method_decorator(canvas_only)
    def get(self, request, *args, **kwargs):
        #self.me = request.facebook.graph.get_object('me')
        form = PoemEntryForm(request.GET)
        return render(request, 'create.html', {
            'form': form,
            'debug': request.session['username']
        })

    #@method_decorator(canvas_only)
    def post(self, request, *args, **kwargs):
        if request.session['username']:
            form = PoemEntryForm(request.POST)
            poem = Poem()
            poem.user = request.session['username']
            poem.text = request.POST['words']
            poem.save()
            return render(request, 'submitted.html', {})
        else:
            return render(request, 'error_submitting.html', {})

提交的.html:

<html>
    <body>
        <h3>You submitted a poem</h3>
        <a href="https://finebitstrings.pythonanywhere.com/">Home</a>
    </body>
</html>
4

1 回答 1

1

所以交易是这样的。当 django 或任何正在回复的回复:missing signed_request.时,它的真正含义是会话不包含 key 条目'signed_request'。您可以在初始请求的 POST 数据中找到此请求。将其保存在会话中,您就可以开始了。

于 2012-09-01T18:49:58.723 回答