Python facebook-sdk 发布到页面

2 投票
2 回答
5432 浏览
提问于 2025-04-18 08:40

我正在尝试用Python的facebook-sdk向我管理的一个Facebook页面发帖,但我不知道该怎么做。我可以这样在我的Facebook动态上发帖:

graph.put_object("me", "feed", message='My message goes here')

我可以这样获取我管理的页面和访问令牌:

fbpages = graph.request('me/accounts')

但是我找不到任何资源来说明如何使用这个访问令牌向页面发帖。我想这可能是这样的:

graph.put_object("me", "page id", "access token", message='My message goes here')

那么我该如何用Python的facebook-sdk来实现这个呢?

我使用的是'pythonforfacebook/facebook-sdk'。

2 个回答

1

使用新的API(版本2.8),你可以用下面的代码:

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()

你可以根据自己的需要来更改附件。我觉得这对需要使用新API的人会很有帮助。首先,你需要先安装Facebook的SDK。

pip install facebook-sdk
5

感谢WizKid给我指明了方向。如果你也遇到这个问题,可以这样解决:

graph = facebook.GraphAPI(page_access_token)
graph.put_object("page id", "feed", message='My message goes here')

撰写回答