简体   繁体   English

Python facebook-sdk发布到页面

[英]Python facebook-sdk post to page

I'm trying to post to a Facebook Page I manage with the Python facebook-sdk but I can't figure out how to do it. 我正在尝试发布到我用Python管理的Facebook页面facebook-sdk,但是我不知道该怎么做。 I can post to my Facebook Wall like so: 我可以这样张贴到我的Facebook Wall:

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

I can get the pages I manage and the access tokens like so: 我可以获得所管理的页面和访问令牌,如下所示:

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

But I can't find any resources to indicate how to actually then use the access token to post to the page. 但是我找不到任何资源来指示如何实际使用访问令牌将其发布到页面。 I imagine it would be something like this: 我想这将是这样的:

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

How would I actually go about doing this with the Python facebook-sdk? 我实际上将如何使用Python facebook-sdk做到这一点?

I'm using 'pythonforfacebook/facebook-sdk'. 我正在使用'pythonforfacebook / facebook-sdk'。

Thank's to WizKid for pointing me in the right direction. 感谢WizKid为我指出正确的方向。 For anyone else with this problem it is solved like so: 对于其他任何有此问题的人,都可以这样解决:

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

With the new API(v2.8), you can use the following code: 使用新的API(v2.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()

You can change the attachment as your need. 您可以根据需要更改附件。 I think this will be helpful if someone needs to use the new API. 我认为如果有人需要使用新的API,这会有所帮助。 You need to install facebook sdk first. 您需要先安装facebook sdk。

pip install facebook-sdk

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM