先决条件:Facebook API Set-1、Set-2、Set-3
在本文中,我们将讨论 3 种方法:
- put_object
- put_like
- put_comment
- put_object:将提到的对象写入连接到给定父级的 Facebook 社交图谱。
参数:- parent_object:指定新对象的父对象的字符串。例如- Post 是评论的父级,当我们要添加新评论或要添加新照片时,用户个人资料是照片的父级。
- 连接名称:指定对象之间的连接或边的字符串。
示例#1:发布带有活动用户墙链接的消息。
import json import facebook def main(): token = "Please replace this with "me" or your Access Token(for Posting on your wall)\ or with PAGE Access Token(for posting on Page)" graph = facebook.GraphAPI(token) message = graph.put_object(parent_object ="me", connection_name ="feed", message ="Hello this is a great website for various Computer Science Topics.", link ="https://www.geeksforgeeks.com") print(json.dumps(message, indent = 4)) if __name__ == '__main__': main()
示例#2:此示例展示了如何使用 put_object 将评论发布到 POST。
import json import facebook def main(): token = "Please replace this with your PAGE ACCESS TOKEN" graph = facebook.GraphAPI(token) commenttopost = graph.put_object(parent_object ="PAGEID_POSTID", connection_name ="comments", message ="Please share and Like the website for content on Computer Science") print(json.dumps(commenttopost, indent = 4)) if __name__ == '__main__': main()
- put_comment :此方法有助于将消息作为注释写入对象。
参数:- object_id:特定资源的唯一字符串。
- 消息:作为评论发布的字符串。
import json import facebook def main(): token = "Please replace this with your PAGE ACCESS TOKEN" graph = facebook.GraphAPI(token) putcomment = graph.put_comment(object_id ="PAGEID_POSTID", message ="This is a very good website for Computer Science Topics") print(json.dumps(putcomment, indent = 4)) if __name__ == '__main__': main()
- put_like :向给定对象添加喜欢
参数:
object_id:一个字符串,它是特定资源的唯一 ID。import json import facebook def main(): token = "Please replace this with your PAGE ACCESS TOKEN" graph = facebook.GraphAPI(token) putlike = graph.put_like(object_id ="PAGEID_POSTID") print(json.dumps(putlike, indent = 4)) if __name__ == '__main__': main()
参考资料: https : //facebook-sdk.readthedocs.io/en/latest/api.html