Facebook应用如何阅读自己的帖子?

时间:2016-07-26 17:15:21

标签: python facebook-graph-api django-facebook

我正在开发一个也可以发布到Facebook的Django应用程序。我在阅读应用程序自己创建的帖子时遇到了问题。

鉴于使用django-allauth请求的权限包括publish_actions,请查看示例代码:

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)

以上所有产生以下输出:

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)

因此,一个应用程序令牌当前可以在我的时间轴中发布,但它无法检索它发布的内容。

我不介意在应用令牌请求时我的Feed显示为空,我有此应用创建的所有帖子ID的记录。我只是希望应用程序访问帖子,他们的喜欢等,而用于应用创建的帖子。

我该怎么做?我错过了什么?

更新

我尝试向用户请求user_posts权限;这会导致/<uid>/feed非空,但第三步仍然失败(我无法通过其ID获取新创建的帖子),所以我的问题仍然适用。

1 个答案:

答案 0 :(得分:0)

我可以按如下方式检索单个帖子。我已经安装了facebook-sdk == 2.0.0并且我已经为我的应用程序授权了user_posts权限。其形式为usernum_postnum。

import facebook

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