djangofacebook-graph-apidjango-facebook

Facebook Graph API & django-facebook - posting links and photos to page feeds


@facebook_required_lazy(scope=['manage_pages', 'publish_stream', 'photo_upload'])
def index(request):
    if request.method == 'POST':
        form = CPYPosterForm(request.POST)

        if form.is_valid():
            fb = require_persistent_graph(request)
            msg = form.cleaned_data['msg']
            pages = form.cleaned_data['pages']

            res = dict()
            for p in pages:
                try:
                    key = PAGE_CHOICES[p]
                except KeyError:
                    key = p
                res[key] = fb.set('{page_id}/feed'.format(page_id=p), message=msg)

I'm trying to post to the feeds of multiple pages. When I post simple text, no problem. However, when I attempt to upload a photo or even a link, the item does get posted to the feed, but it's to the "Recent Posts by Others" section and not the page feed itself.

It gets posted as me and not as the page. Is there something I need to do special to post an item in the pages feed (as the page) with a link and/or photo?


Solution

  • Facebook Graph API PHP SDK posting on page as page

    Found it. There is a different access token for impersonating the page to post as the page. You get that from /me/accounts and use that token to post as the page. Updated working code:

    @facebook_required_lazy(scope=['manage_pages', 'publish_stream', 'photo_upload'])
    def index(request):
        if request.method == 'POST':
            form = CPYPosterForm(request.POST)
    
            if form.is_valid():
                fb = require_persistent_graph(request)
                msg = form.cleaned_data['msg']
                pages = form.cleaned_data['pages']
    
                me_accounts = fb.get('/me/accounts')
                access_token = None
    
                resp = dict()
                for p in pages:
                    for d in me_accounts['data']:
                        if d['id'] == p:
                            access_token = d['access_token']
                    try:
                        key = PAGE_CHOICES[p]
                    except KeyError:
                        key = p
    
                    openfb = OpenFacebook(access_token)
                    resp[key] = openfb.set('{page_id}/feed'.format(page_id=p), message=msg, link='http://www.espn.com')