Prerequisites: Facebook API Set-1, Set-2, Set-3
In this article we will be discussing 3 methods :
- put_object
- put_like
- put_comment
- put_object: Writes the mentioned object to the Facebook Social Graph connected to the given parent.
Parameters:- parent_object: A string specifying the parent of the new object. For example- Post is the parent of comment, when we want to add a new comment or when we want to add a new photo, User profile is the parent of the photo.
- connection name: A string specifying the connection or edges between the objects.
Example#1: Post a message with a link to the active user wall.
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."
,
print
(json.dumps(message, indent
=
4
))
if
__name__
=
=
'__main__'
:
main()
Example#2: This example shows how to use put_object to post a comment to a 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: This methods facilitates to write a message as comment to an object.
Parameters:- object_id: A string unique for a particular resource.
- message: A string to be posted as comment.
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: Add like to a given object
Parameters:
object_id: A string that is a unique id for the particular resource.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()
References: https://facebook-sdk.readthedocs.io/en/latest/api.html