Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
DelphiRAD StudioTech Partner

How To Make Facebook Posts From Windows And Mobile Apps

How To Make Facebook Posts From Windows And Mobile Apps

At Softacom, as a software company specializing in mobile and Windows app development using Delphi, we often integrate clients’ software with 3rd party services using API. In this article we show you how to automatically create posts on Facebook from your Delphi apps using the Facebook Graph API.

How can I create posts on Facebook from a Delphi application?

How To Make Facebook Posts From Windows And Mobile Apps

Facebook is the world’s most popular social platform used by businesses to communicate with their clients, and not only.
The daily amount of information (texts, videos, images, urls) being posted in Facebook business accounts is huge and keeps growing. That’s why big companies often apply software solutions to distribute content effectively and in time.
In this article we’ll show you how to automate the process of content posting and updating through Facebook API (namely Facebook Graph API).
We’ll develop a Delphi FMX software application to post on a user’s Facebook page.
The whole process can be split into the following steps:

  • Create a Facebook app
  • Make the required adjustments in the app
  • Debug and test queries using the built-in Facebook API debugging tool called Graph API Explorer
  • Develop a Delphi Fmx (cross-platform) software application
  • Post some content on a Facebook page

To work with Facebook Graph API we’ll use GET, POST and DELETE methods and requests. The server response will be in JSON format.

How to create a Facebook application entry

To work with Facebook Graph API you first need to sign in with your Facebook account (sign up if you do not have one). Then you need to go https://developers.facebook.com and register as a developer.

facebook_app_1

Then you need to confirm your email address

facebook_app_2

Select the profile type and click “Complete registration”.

facebook_app_3

To start using the Facebook API you need to create a facebook app in My Apps .

facebook_app_4

Select “Company” application type.

facebook_app_5
facebook_app_6

Next, enter the application name, select its purpose and click Create App.

facebook_app_7

Next enter your Facebook login password

facebook_app_8

Congrats! You’ve just created a Facebook app. 

How do I get an access token so I can write my own apps to post to Facebook?

Now we need to get a token to access the page and configure the appropriate permissions to start posting.

facebook_app_9

To get the token run Graph API Explorer.

facebook_app_10

The Graph API Explorer will help us get the access token so that we can execute test queries and post to the page ( Softacom’s test page).

facebook_app_11

To work with the Facebook app select it from the drop-down lis. To get the token to access the page, choose Get Page Access Token from the User or Page.

facebook_app_12

Next, click Continue

facebook_app_13

Select the page you need and save the ID for further posting:

facebook_app_14

Both options have to be turned on. In our case Softacom. Click Further.

facebook_app_15
facebook_app_16

The access token has been generated.

How do I check my app can use the Facebook Graph API?

We can test the Facebook Graph API with the built-in debugging tool “Graph API Explorer”.

First, let’s test a GET request to get the profile name and identification number.

facebook_app_17

Select your page (Softacom)

facebook_app_18

Set up special permissions for posting (publish_to_groups, page_manage_posts).

facebook_app_19
facebook_app_20

Click “Generate Access Token” to generate an access token in order to save the permissions settings.

facebook_app_21
facebook_app_22

Permissions are saved successfully and a new access token has been generated.

facebook_app_23

Now select your page from the list

facebook_app_24

Send the GET request to get the page name and ID to ensure the Facebook Graph API works correctly.

facebook_app_25

Send a test POST request to add a post to the page in  form of a text message.

facebook_app_26

Now refresh the page and you should see the text message posted on the page.

facebook_app_27

To delete a post, execute a DELETE request using the id of our post

facebook_app_28

You should receive a response in JSON format telling that the post was deleted successfully.

facebook_app_29

The previously received access token to the page is valid for an hour. To continue using the Facebook Graph API capabilities in Embarcadero Delphi’s FMX application, should obtain a long-term access token.

How do I get a permanent access token to allow my app to post to Facebook?

To get a permanent access token, press “i” in front of our temporary token.

facebook_app_30

Next Open in Access Token Tool

facebook_app_31

Choose Extend Access Token

facebook_app_32
facebook_app_33

Copy and save the resulting token

facebook_app_34

Where do I find an example of the Facebook API code?

For further usage of requests in the Embarcadero Delphi FMX application, you need to get the request body using Get Code

facebook_app_35
facebook_app_36

The request body must be copied and saved for later usage in Delphi. Similarly, can get the code of the remaining required requests.

How do I build a Delphi FMX app to post on a Facebook page?

To make requests to the Facebook API and receive a response in JSON format, use the TNetHttpRequest and TNetHttpClient components from the component palette (Net tab). They can be easily dragged onto the form. To work correctly, you need to change certain settings for the components. In the TNetHttpRequest component, in the Client field, select the current TNetHttpClient component that was added to the form along with TNetHttpRequest. You also need to set the MethodString parameter to POST.

facebook_app_37

To enter messages use the TMemo component.

facebook_app_38

TEdit components are used to enter image URLs, links for posting on a Facebook page. To enter an id, by which you’ll be able to delete a post from the page in future if needed, also use the TEdit component.

facebook_app_39

The response from the server in the form of JSON will be stored in the TMemo component.

facebook_app_40

In the onCreate form event handler, we assign the page access token to a variable of type string (token)

facebook_app_41

The text message entered by the user is stored in the “mes” string variable.

facebook_app_42

The post parameters (message, link, image url) are passed using a POST request to the Facebook Graph API server. Delphi uses the TMultiPartFormData class to send data using the POST method.

facebook_app_43

Next, the request with parameters is sent using the NetHttpRequest POST method, where the input parameters are the url address of the server, an object of the TMultipartFormData type with parameters to pass to the server, and a TMemoryStream object to store the data received from the server.

facebook_app_44

Code snippets of button handlers for posting a simple text message, updating a post, posting an image url, deleting a post, posting a message and a link are given below.

How do I test my new Facebook Delphi app?

The app’s operation on the MSWindows platform is shown below. Posting a simple text message

facebook_app_45
facebook_app_46

Below is the result of posting a text message on the Softacom page

facebook_app_47
facebook_app_48
facebook_app_49

The result of posting a text message and a link on the Softacom page

facebook_app_50

Can I delete Facebook posts made by my Delphi app?

If desired, you can delete the post from the Facebook page using post Id. To do this, use the capabilities of the FMX application.

facebook_app_51

Enter Id and click “Delete post by id”.

facebook_app_52

The post on the Softacom page has been successfully deleted.

facebook_app_53

Softacom is one of Embarcadero’s Tech Partners. If you would like to try Delphi for yourself, why not download a free trial copy of RAD Studio today?


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Related posts
CodeDelphi

10 Signs You Should Invest in Cross Platform Apps

C++Delphi

What Are Cross Platform Programming Languages?

C++IDERAD Studio

The Top 3 CLANG Based C++ IDE Choices Right Now

C++

How To Build Desktop Apps For Windows?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES