发布到页面的Python facebook SDK权限错误
问题描述:
我创建了一个Facebook页面,并且正在尝试使用下面的代码在页面墙上发布信息。它是用Python编写的,并使用Facebook SDK。发布到页面的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开发者控制台上创建一个应用程序: https://developers.facebook.com/apps。提供显示名称和电子邮件,点击创建应用程序ID。
- 转到设置>基本>选择类别(页面应用)>保存更改。
- 转到应用程序审查>使'AppName'公开在顶部>切换到是,然后确认。这将使应用程序公开。
- 重新转到仪表板并记下应用程序ID和应用程序密钥(点击它旁边的显示,它会询问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小时内到期。在这种情况下,您可以生成一个长寿命的令牌或永久令牌。
+0
我在评论中使用了WizKid提供的解决方案。我缺少publish_pages权限。这解决了这个问题。 – pratibha
用户需要授予应用程序publish_pages才能在页面上发布 – WizKid
非常感谢您,我一直在努力工作6个小时来实现这一目标。 – pratibha
请将其发布为答案,以便我可以将其标记为已接受。 – pratibha