Here we are going to discuss how to publish a post on Facebook with Facebook graph API using python
First We are Going to Getting Know about What is Facebook Graph APIs
The Facebook Graph API exposes URL endpoints to which you can make GET and POST calls to interact with your Facebook page or upload data and posts to your Facebook account. The Graph API is the primary way for apps to read and write to the Facebook social graph.
using Facebook Graph API,
- Read the messages on your timeline,
- Send messages,
- Read posts,
- Create posts, edit posts or even delete posts and etc…
Before You Start, You will need ( see video tutorial ):
- Register as a Facebook Developers
- Create a Facebook App
- Open the Graph Explorer tool ( this tool lets you make calls to Facebook’s Graph API)
- Get brief understanding of the structure of the Facebook Social Graph from our Graph API Overview guide
** Main Step 01 **( Create a Facebook App )
** Main Step 02 ** ( Get your Facebook Page Access Tokens )
see this video https://vimeo.com/701609024
** Main Step 03 ** ( Send Request Using Python )
We will be using the requests library for sending the HTTP requests. Requests allow you to send HTTP/1.1 requests extremely easily. There’s no need to manually add query strings to your URLs or to form-encode your PUT & POST data — but nowadays, just use the json method!
python -m pip install requests
- You will also need a page ID — for the Facebook page you would like to post to. You can get your page ID directly from your Facebook page, under the “About” tab. Copy this ID and paste it into your code.
- This is the code for creating a post
import requests
#Your Access Keys
page_id_1 = 123456789
# Your Page Access Token
facebook_access_token_1 = ‘Your Page Access Token’
# Post Content as Text
msg = ‘hi buddy’
post_url = ‘https://graph.facebook.com/{}/feed'.format(page_id_1)
payload = {
‘message’: msg,
‘access_token’: facebook_access_token_1
}
r = requests.post(post_url, data=payload)
print(r.text)
Thank you!
Top comments (3)
Hi, I followed your manual but I'm getting this error message.
It seems I have to have more privileges?
{"error":{"message":"(#200) If posting to a group, requires app being installed in the group, and \\\n either publish_to_groups permission with user token, or both pages_read_engagement \\\n and pages_manage_posts permission with page token; If posting to a page, \\\n requires both pages_read_engagement and pages_manage_posts as an admin with \\\n sufficient administrative permission","type":"OAuthException","code":200,"fbtrace_id":"XXXXXXXX"}}
Okay... I skipped one small detail from the video :-) It works
Hi, I would to ask about can I use it to post in groups I'm not the admin of it just a member