Some links in this post may be affiliate links. We may get paid if you buy something or take an action after clicking one of these, but without addictional costs for you compared to direct buying.

Facebook Robot to Post on Pages with Raspberry PI

Raspberry PI Facebook Graph API featured image
0
(0)

Facebook is the world most used social platform. In some cases, having a python robot able to post your messages can help with managing a lot of pages. Facebook Graph API, with Raspberry PI, can help this job

In this tutorial I’m going to show you how to install Facebook Graph API in your Raspberry PI and publish posts on Facebook pages.

Facebook Graph API is the main (and official) way to develop applications able to interface and interact with your Facebook. You can use them to read and write to Facebook social graph.

All official Facebook products and software development kits interact using Graph API.

In this tutorial I’m going to use a cheap Raspberry PI Zero W, but this guide applies to all Raspberry PI computer boards.

What We Need

As usual, I suggest adding from now to your favorite e-commerce shopping cart all needed hardware, so that at the end you will be able to evaluate overall costs and decide if continuing with the project or removing them from the shopping cart. So, hardware will be only:

Check hardware prices with following links:

Amazon raspberry pi boards box
Amazon raspberry pi Zero W box
Amazon Micro SD box
Amazon Raspberry PI Power Supply box

Step-by-Step Procedure

Getting API token

Before writing our python script, we need to get (free) access in Facebook Developer Portal and registering our new app, to get a token able to give our script right permissions.

From Facebook for Developers website, click Get Started button:

Facebook Developer API registration - 01
Ref: Facebook Developers Docs

Login using your facebook credentials and compile needed fields according to your preferences.

If you are coming from a first time registration flow, you will be redirected to a welcome page. Use “Create First App” button, whose name well explains its goal (otherwise, go to the Apps panel and click “Create App”):

Facebook Developer API registration - 02
Ref: Facebook Developers Docs

In next window, use the last (“None”) option as app type:

Add your app name and email address. You can leave Business Manager option with its default:

Once clicked final “Create App” button, you have completed the app creation flow and your app will be loaded in next Dashboard:

Go to Tools -> API Graph Explorer. Your new screen will show on right sidebar the Access Token menu:

Here resided the most important part of this procedure. You need to create the right toke with right permissions to make your script working.

In Facebook App, select your created app name.

With drop-down menu, add following permissions:

  • pages_show_list
  • pages_read_engagement
  • pages_manage_posts

In bottom part of this sidebar, if Access Token field is still empty, click “Get Access Token” to generate an alphanumeric token.

In “User or Page” menu, scroll down to Page access token and select your Facebook page name.

The resulting token is valid for our purposes but will have a low duration validity. To extend token expiration date, click on “i” icon near the token field:

You will get a popup with access token info. Click “Open in Access Token Tool” button:

Facebook Developer API registration - 08

Scroll Down and click “Extend Access Token” button. This will show a new token:

Facebook Developer API registration - 09

Copy the token and save with you, as it will be used in your Python script.

Get Facebook Page ID

Facebook Page ID is another required parameter. You can find it in your Facebook page, in “Info” section. This is a number like “687947481647282”, which is Page Id for my peppe8o Facebook page.

Prepare your Raspberry PI OS

Finally, we are to funny stuff. Install your operating system, using Raspberry PI OS Lite (for a fast, headless OS) or Raspberry PI OS Desktop (in this case working from its internal terminal).

Install pip and then facebook-sdk with requests package to use Facebook Graph API:

sudo apt install python3-pip
python3 -m pip install facebook-sdk requests

Create Publish Script

You can create your own script with following instructions, which will explain line by line its code, or get a copy directly in your Raspberry PI, editing only token and page variables, with the following terminal command:

wget https://peppe8o.com/download/python/facePagePost.py

This script starts, as usual, importing required modules:

from facebook import GraphAPI

The next lines associate your token (a very long string) to a variable (easier to manage) and connect to Facebook Graph API. Please remember to use your token (copied between brackets) instead of “your_Token_Goes_Here” string:

tk = 'your_Token_Goes_Here'
graph = GraphAPI(access_token=tk)

Preconfigure your message, by composing text (associated to postMsg variable), link (if needed, associated to postLink variabel) and page ID. Please remember to use your Facebook page Id instead of mine:

postMsg='Hello people from my Raspberry PI!'
postLink='http://peppe8o.com'
pageId='687947481647282'

Final line runs the publish command, using previous variables. You can also avoid adding a link (if not needed), by removing last “link” option):

graph.put_object(pageId,"feed", message=postMsg, link=postLink)

Once modified with your token and page ID, you can run this script with following command:

python3 facePagePost.py

After execution, check your Facebook page for your published post.

Enjoy!

How useful was this post?

Click on a star to rate it anonymously!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

I accept the Privacy Policy