After this these steps you will be able to automate the process of posting on Facebook by a Python script. In this example I will show how it is done on a Facebook brand page, Learn Python With Rune.
What you need.
- A graph API token, which you by registering as a developer on facebook and creating an App there.
- Make a simple Python program using the facebook library
Step 1: Registering as developer at Facebook
To register as a developer at Facebook you need to log in to developer.facebook.com
You press the Log In in the top right corner and log in with you Facebook credentials.
Step 2: Create an App
You need to create an App to get the graph API token.
Under My Apps you press Create App.
Press the Manage Pages, Ads or Groups.
You enter App Display Name, which will be the name that is used when posting from this App. Hence, chose a name that you like people to see in the post.
Fill out your email (probably it is automatically there) and press Create App ID.
Step 3: Create Graph API token
Under tools choose Graph API explorer
Ensure that the right Facebook App is chosen. Then under User or Page chose get Page Access Token.
It will prompt you to log in to your Facebook account and ask permission for sharing your page.
Agree with that.
Then you will get back to this screen.
Where you want to add pages_manage_posts, that will grant you access to create posts.
Then click Generate Access Token and you will be prompted to agree with the new access rights on your Facebook page.
Step 4: Prolong you graph API token
The graph API token is quite short lived, so you want to extend it.
Press the info at the graph API token.
Then press the Open in Access Token Tool.
Where you in the bottom will find Extend Access Token. Press that.
Step 6: Install facebook-sdk library
To make you life easy in Python, you need to install the facebook-sdk library.
pip install facebook-sdk
Step 7: The Python magic
You need to insert you Access Token in the code.
Also, insert the page ID you want. You can find your Page ID with this page.
import facebook page_access_token = "" # Replace with you access token graph = facebook.GraphAPI(page_access_token) facebook_page_id = "" # insert you page ID here. graph.put_object(facebook_page_id, "feed", message='test message')
That’s it. Enjoy.
Do you know what the 5 key success factors every programmer must have?
How is it possible that some people become programmer so fast?
While others struggle for years and still fail.
Not only do they learn python 10 times faster they solve complex problems with ease.
What separates them from the rest?
I identified these 5 success factors that every programmer must have to succeed:
- Collaboration: sharing your work with others and receiving help with any questions or challenges you may have.
- Networking: the ability to connect with the right people and leverage their knowledge, experience, and resources.
- Support: receive feedback on your work and ask questions without feeling intimidated or judged.
- Accountability: stay motivated and accountable to your learning goals by surrounding yourself with others who are also committed to learning Python.
- Feedback from the instructor: receiving feedback and support from an instructor with years of experience in the field.
I know how important these success factors are for growth and progress in mastering Python.
That is why I want to make them available to anyone struggling to learn or who just wants to improve faster.
With the Python Circle community, you can take advantage of 5 key success factors every programmer must have.
Be part of something bigger and join the Python Circle community.