发布到页面的Python facebook SDK权限错误

发布到页面的Python facebook SDK权限错误,python,facebook,python-3.x,facebook-graph-api,facebook-sdk-4.0,Python,Facebook,Python 3.x,Facebook Graph Api,Facebook Sdk 4.0,我已经创建了一个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 = grap

我已经创建了一个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。有人能帮我吗?

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

  • 首先在facebook开发者控制台上创建一个应用程序: . 提供显示名称和电子邮件,单击创建应用程序ID。
    • 转到设置>基本>选择类别(页面应用)>保存更改
    • 转到应用程序审查>在顶部公开“AppName”>切换到是,然后确认。这将使应用程序公开
    • 再次转到仪表板并记下应用ID和应用机密(单击它旁边的显示,它将询问fb密码)
现在生成访问令牌:

  • 后藤。在应用程序下拉列表中>选择先前创建的应用程序>从获取令牌>获取用户访问令牌>从选择权限>检查管理页面、发布操作、发布页面>获取访问令牌
  • 单击continue as user(用户名),将生成令牌
  • “访问令牌”字段中显示的令牌是您的短期访问令牌。保存此令牌,因为生成长寿命令牌将需要此令牌
这是一个短期令牌,将在两小时内过期。保存令牌。安装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小时内过期。在这种情况下,您可以生成长寿令牌或永久令牌。

用户需要授予应用程序发布页面以在页面上发布感谢您,我为此奋斗了6个小时。请将其作为答案发布,以便我可以将其标记为已接受。我在评论中使用了WizKid提供的解决方案。我错过了发布页面的许可。这解决了问题。