我创建了一个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:Python34libsite-packagesfacebook_sdk-3.0.0a0-py3.4.eggfacebook__init__.py", line 188, in put_wall_post
File "C:Python34libsite-packagesfacebook_sdk-3.0.0a0-py3.4.eggfacebook__init__.py", line 169, in put_object
File "C:Python34libsite-packagesfacebook_sdk-3.0.0a0-py3.4.eggfacebook__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。
- goto设置>基本>选择类别(页面的应用程序)>保存更改。
- goto应用程序评论>在顶部公开" appname">" toggle to yes",然后确认。这将使应用程序公开。
- 再次goto仪表板,并记下应用ID和应用程序秘密(单击它旁边的显示,它会要求FB密码)。
现在生成访问令牌:
- goto https://developers.facebook.com/tools/explorer。在"应用程序"下拉>中>在" get token">"获取用户访问令牌">" select权限"> Check 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小时内到期。在这种情况下,您可以产生长期的持续令牌或永久令牌。