首页 文章

Python Facebook图形API发布带预览的URL

提问于
浏览
1

我想使用Graph API在我的Facebook墙上发布一个链接 . 当我手动将URL放在"Write Something"文本框中时,它会显示URL的预览并将其发布 .
Manual Post on FB wall

我尝试通过图形api发布相同的URL

import facebook

graph = facebook.GraphAPI(access_token)
graph.put_wall_post("http://www.channelnewsasia.com/news/world/7-year-old-writes-to-google-for-job-ceo-sundar-pichai-replies/3526608.html")

但它将链接发布为文本 . 我正在使用Facebook-SDK访问Facebook图形API .

如何从Graph API实现相同的功能?

1 回答

  • 3

    如果您要共享URL,则需要使用附件参数,以便在帖子中显示缩略图预览 . 您必须按照API参考建议的以下方式添加它:

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

    如果将profile_id留空,则默认为您的 Profiles . 在附件字典中,您可以保留不需要的额外字段 .

相关问题