tzot
tzot

Reputation: 95961

How can a Facebook app read its own posts?

I'm developing a Django application that can also post to Facebook. I have a problem reading the posts that the application itself created.

Given that the permissions requested using django-allauth includes publish_actions, check the example code:

import open_facebook as ofb
import time

# the following app access token is retrieved by:
# curl -F type=client_cred -F client_id=<my_application_id> \
#      -F client_secret=<my_application_secret> \
#      https://graph.facebook.com/oauth/access_token
APP_ACCESS_TOKEN= "<my_application_id>|blahblah_blahblah"

MY_FACEBOOK_UID= "<my_facebook_user_id>"

fb= ofb.OpenFacebook(APP_ACCESS_TOKEN)

# this works fine! see output later
print("adding a post…")
data= fb.set("/%s/feed" % MY_FACEBOOK_UID,
        message="a test post, will be deleted\n" + time.asctime(),
        link="some_link_to_my_application",
        application="<my_application_id>")
print("data returned:")
print(data)
fbpost_id= data['id']

# this works too! it comes up empty, but I don't mind
print("getting feed…")
data= fb.get("/%s/feed" % MY_FACEBOOK_UID)
print("result:")
print(data)

# and this does not work.
print("getting post…")
data= fb.get(fbpost_id) # neither this or ("/%s" % fbpost_id) work
print("data returned:")
print(data)

All of the above produces the following output:

adding a post…
data returned:
{'id': '<a_very_nice_id_thank_you_fb>'}
getting feed…
result:
{'data': []}
getting post…
FB request, error type <class 'urllib.error.HTTPError'>, code 400
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/lib/python3/dist-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/usr/lib/python3/dist-packages/django/core/management/__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/python3/dist-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/lib/python3/dist-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/home/<blahblah>/management/commands/blahtest.py", line 41, in handle
    data= fb.get(fbpost_id)
  File "/usr/local/lib/python3.5/dist-packages/open_facebook/api.py", line 731, in get
    response = self.request(path, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/open_facebook/api.py", line 937, in request
    response = self._request(url, post_data)
  File "/usr/local/lib/python3.5/dist-packages/open_facebook/api.py", line 245, in _request
    parsed_response['error'].get('code'))
  File "/usr/local/lib/python3.5/dist-packages/open_facebook/api.py", line 338, in raise_error
    raise error_class(error_message)
open_facebook.exceptions.ParameterException: Unsupported get request. Object with ID '<a_very_nice_id_thank_you_fb>' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api (error code 100)

So, an application token currently CAN post in my timeline BUT it can't retrieve what it posted.

I don't mind if my feed shows empty when requested by the app token, I have a record of all post ids created by this app. I just want the application to access posts, their likes etc, and only for the posts the app created.

How can I do that? What am I missing?

Update

I tried requesting the user_posts permission from the user; this results in the /<uid>/feed coming non-empty, but the third step still fails (I can't get the newly-created post by its id), so my question still applies.

Upvotes: 0

Views: 91

Answers (1)

Jessamyn Smith
Jessamyn Smith

Reputation: 1649

I am able to retrieve single posts as follows. I have installed facebook-sdk==2.0.0 and I've authorized the user_posts permission for my application. The is of the form usernum_postnum.

import facebook

graph_api = facebook.GraphAPI(access_token=access_token, version='2.7')
graph_api.request('<facebook_post_id>')

Upvotes: 0

Related Questions