【问题标题】:Python facebook SDK permission error for post to page发布到页面的 Python facebook SDK 权限错误
【发布时间】:2017-02-20 03:47:14
【问题描述】:

我创建了一个 facebook 页面,并尝试使用以下代码在页面墙上发布。它是用 python 编写的,使用 facebook SDK。

def main():
  cfg = {
    "page_id"      : "PAGE_ID",  # Step 1
    "access_token" : "USER_ACCESS_TOKEN"   # Step 3
    }

  graph = facebook.GraphAPI(cfg['access_token'])
  id = graph.get_object('me')['id']
  print(graph.get_permissions(user_id=id))
  resp = graph.get_object('me/accounts')

  page_access_token = None
  for page in resp['data']:
      if page['id'] == cfg['page_id']:
          page_access_token = page['access_token']
  api = facebook.GraphAPI(page_access_token)

  msg = "Hello, world!"
  print(api.get_permissions(user_id=id))
  print(api.put_wall_post(msg))

下面的输出有错误:

{'user_posts', 'publish_actions', 'public_profile', 'pages_manage_cta', 'manage_pages'}
{'user_posts', 'publish_actions', 'public_profile', 'pages_manage_cta', 'manage_pages'}
    print(api.put_wall_post(msg))
  File "C:\Python34\lib\site-packages\facebook_sdk-3.0.0a0-py3.4.egg\facebook\__init__.py", line 188, in put_wall_post
  File "C:\Python34\lib\site-packages\facebook_sdk-3.0.0a0-py3.4.egg\facebook\__init__.py", line 169, in put_object
  File "C:\Python34\lib\site-packages\facebook_sdk-3.0.0a0-py3.4.egg\facebook\__init__.py", line 298, in request
facebook.GraphAPIError: (#200) The user hasn't authorized the application to perform this action

我不明白我做错了什么?我正确地授予了用户权限。我检查了其他重复的问题,但他们的解决方案不适用于当前的 facebookSDK。有人可以帮帮我吗?

【问题讨论】:

  • 用户需要授予应用 publish_pages 才能在页面上发布
  • 非常感谢,我为此奋斗了 6 个小时。
  • 请将其发布为答案,以便我将其标记为已接受。

标签: python facebook python-3.x facebook-graph-api facebook-sdk-4.0


【解决方案1】:

对于您要发布的页面,转到页面上的“关于”选项卡并在底部获取页面 ID。记下或保存。那么,

  • 首先在 facebook 开发者控制台上创建一个应用程序: https://developers.facebook.com/apps。提供显示名称和电子邮件,单击创建应用程序 ID。
    • 转到设置>基本>选择类别(页面的应用程序)>保存更改。
    • Goto App Review > Make ‘AppName’ Public at top > 切换到是,然后确认。这将使应用公开。
    • 再次转到 Dashboard 并记下 App ID 和 App Secret(单击旁边的显示,它会要求输入 fb 密码)。

现在生成访问令牌:

  • 转到https://developers.facebook.com/tools/explorer。在应用程序下拉菜单中 > 选择之前创建的应用程序 > 从获取令牌 > 获取用户访问令牌> 从选择权限 > 检查 manage_pages、publish_actions、publish_pages > 获取访问令牌。
  • 单击以用户身份继续(用户名),将生成令牌。
  • 出现在“访问令牌”字段中的令牌是您的短期访问令牌。保存此令牌,因为生成长期令牌需要此令牌。

这是一个短期令牌,将在两小时内到期。保存令牌。安装 facebook sdk:

pip install facebook-sdk

使用以下代码:

import facebook

def main():
  # Fill in the values noted in previous steps here
  cfg = {
  "page_id"      : "value of the page id",  # Step 1
  "access_token" : "token you generated"   # Step 3
  }

api = get_api(cfg)
msg = "Hello, world!"
status = api.put_wall_post(msg)

def get_api(cfg):
  graph = facebook.GraphAPI(cfg['access_token'])
  # Get page token to post as the page. You can skip 
  # the following if you want to post as yourself. 
  resp = graph.get_object('me/accounts')
  page_access_token = None
  for page in resp['data']:
     if page['id'] == cfg['page_id']:
     page_access_token = page['access_token']
     graph = facebook.GraphAPI(page_access_token)
     return graph

if __name__ == "__main__":
   main()

或者你可以简单地使用以下内容:

import facebook
def main():
    graph = facebook.GraphAPI(access_token='your_user_access_token', version='2.8')
    #if version 2.8 show error use 2.6
    attachment =  {
        'name': 'Link name'
        'link': 'https://www.example.com/',
        'caption': 'Check out this example',
        'description': 'This is a longer description of the attachment',
        'picture': 'https://www.example.com/thumbnail.jpg'
    }

    graph.put_wall_post(message='Check this out...', attachment=attachment, profile_id='your_page_id')

if __name__ == "__main__":
    main()

请记住,令牌将在 2 小时内过期。在这种情况下,您可以生成长期存在的令牌或永久令牌。

【讨论】:

  • 我在 cmets 中使用了 WizKid 提供的解决方案。我缺少 publish_pages 权限。这解决了问题。
猜你喜欢
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
  • 2017-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多