Python Facebook tutorial - post to Facebook page in 4 steps
In
this tutorial, you will learn how to post to a Facebook page's wall
(acting as the page) using Python. It includes step by step instructions
to create a new page, register an app with Facebook and some python
code. I have a similar Python tutorial for Twitter.
Phew, all done!
Step 1
- First, create a new Facebook page. Select appropriate page type, fill in description and other relevant fields.
- On the new page, go to
About
tab, and note theFacebook Page ID
. - We will post to this page's wall, acting as the page.
Step 2
- Now create a Facebook App which will be used to access Facebook's Graph API.
- Go to Facebook Apps dashboard -> Click
Add a New App
-> Choose platformWWW
-> Choose a new name for your app -> ClickCreate New Facebook App ID
->Create a New App ID
-> ChooseCategory
(I chose "Entertainment") -> ClickCreate App ID
again. - Go back to Apps dashboard -> Select the new app ->
Settings
->Basic
-> EnterContact Email
. This is required to take your app out of the sandbox. - Go to
Status & Review
->Do you want to make this app and all its live features available to the general public?
-> Toggle the button toYes
->Make App Public?
->Yes
. This will enable others to see posts by your app in their timelines - otherwise, only you will see the wall posts by the app. This took me some very frustating hours to figure out! These two stackoverflow posts were very helpful. - Now - you should see a green dot next to app's name, and the text
This app is public and available to all users
. - Make a note of the
App ID
andApp Secret
(ClickShow
next to it; you will be asked to re-enter your Facebook password).
Step 3
- In this step we will obtain obtain Facebook OAuth token. A
long-lived
token at that! Read about Facebook access tokens. Some more helpful articles about Facebook token process. - Go to Graph API Explorer -> In the
Application
drop down -> Select the app created in Step 2 -> ClickGet Access Token
-> InPermissions
popup go toExtended Permissions
tab -> Selectmanage_pages
, andpublish_actions
These permissions will allow your app to publish posts acting as the page -> ClickGet Access Token
-> You will see a message saying "{App} would like to post publicly to Facebook for you. Who do you want to share these posts with?" -> I chosePublic
for maximum visibility - as I wanted to post to a public page. - You might be asked to
Turn On Platform
if you disabled it previously, enable it! If you mess this step up, just go to your App Settings - remove the app and try again. - Make a note of the
short-lived
token shown in Graph API Explorer. - Facebook has deprecated offline access, the next best thing is
long-lived
token which expires in 60 days. We will convert theshort-lived
access token noted above to along-lived
token. This helped me figure it out.
https://graph.facebook.com/oauth/access_token?
client_id={APP_ID}&
client_secret={APP_SECRET}&
grant_type=fb_exchange_token&
fb_exchange_token={EXISTING_ACCESS_TOKEN}
- You should see
access_token={...}&expires={...}
. This new access_token is thelong-lived
token we will use in our Python script. long-lived
token will also expire eventually, be prepared to perform this Step 3 again before that happens! If you do not want to deal with that just save thepage_access_token
computed in Step 4 - and you can use it forever, as according to Facebook's documentation a page access token obtained fromlong-lived
user token will not have any expiry time.
Step 4
- We will use Facebook Python SDK to access Facebook's Graph API. You can install it using pip:
pip install facebook-sdk
(again, use of virtualenv is highly recommended).
import facebook
def main():
# Fill in the values noted in previous steps here
cfg = {
"page_id" : "VALUE", # Step 1
"access_token" : "VALUE" # Step 3
}
api = get_api(cfg)
msg = "Hello, world!"
status = api.put_wall_post(msg)
def get_api(cfg):
graph = facebook.GraphAPI(cfg['access_token'])
# Get page token to post as the page. You can skip
# the following if you want to post as yourself.
resp = graph.get_object('me/accounts')
page_access_token = None
for page in resp['data']:
if page['id'] == cfg['page_id']:
page_access_token = page['access_token']
graph = facebook.GraphAPI(page_access_token)
return graph
# You can also skip the above if you get a page token:
# http://stackoverflow.com/questions/8231877/facebook-access-token-for-pages
# and make that long-lived token as in Step 3
if __name__ == "__main__":
main()
Next steps
Allow multiple people to log-in to the app? I am not sure how to exactly do it, but here are a few pointers:- First step would be to add a login dialogue - instead of Step 3. This will be a popup from facebook.com which will show which permissions your app is requesting.
- The 'login' API call will return a
short-lived
token. This is the tricky part, user is viewing a facebook.com page right now, how will your app get the auth token? Might be easier to do using their Javascript login flow. Other option is to give a redirect URL - which will be called when the popup is closed (and token added as a parameter). - Once you get the
short-lived
token, you can follow the tutorial - convert tolong-lived
and store that in a database - sqlite3 works very well for small prototypes. - Read these two links to understand more: Facebook.com - Manual login flow, Facebook.com Web login flow (javascript).
- If required, renew the
long-lived
token for your users.
0 comments :