2

I'm using the django_facebook library installed on pythonanywhere. When I click a link from one view to another, I get an error message:

400 Bad Request

Missing signed_request.

Could someone give me the brief on how links work in a facebook app?

Removing the @canvas_only decorator doesn't solve the problem, because I need access to the graph api.

Here's the code:

views.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', {})

submitted.html:

<html>
    <body>
        <h3>You submitted a poem</h3>
        <a href="https://finebitstrings.pythonanywhere.com/">Home</a>
    </body>
</html>
lowerkey
  • 8,105
  • 17
  • 68
  • 102

1 Answers1

1

So the deal is this. When the django, or whatever is doing the replying replies with: missing signed_request., what it really means is that the session doesn't contain an entry of key 'signed_request'. You can find this request in the POST data of the initial request. Save it in the session, and you're good to go.

lowerkey
  • 8,105
  • 17
  • 68
  • 102