![| Embarcadero RAD Studio Delphi C++Builder Blogs How To Make Facebook Posts From Windows And Mobile Apps](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/02/pexels-cottonbro-5077038-2579261-scaled.jpg?resize=1140%2C694&ssl=1)
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.
Table of Contents
How can I create posts on Facebook from a Delphi application?
![| Embarcadero RAD Studio Delphi C++Builder Blogs How To Make Facebook Posts From Windows And Mobile Apps](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/iMac-16-1-6255450.jpg?resize=750%2C454&ssl=1)
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.
![facebookapp1 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 1](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_1-9242550.jpeg?resize=750%2C400&ssl=1)
Then you need to confirm your email address
![facebookapp2 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 2](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_2-2623851.jpeg?resize=750%2C400&ssl=1)
Select the profile type and click “Complete registration”.
![facebookapp3 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 3](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_3-9370732.jpeg?resize=750%2C400&ssl=1)
To start using the Facebook API you need to create a facebook app in My Apps .
![facebookapp4 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 4](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_4-5231552.jpeg?resize=750%2C400&ssl=1)
Select “Company” application type.
![facebookapp5 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 5](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_5-4176763.jpeg?resize=750%2C400&ssl=1)
![facebookapp6 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 6](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_6-9828919.jpeg?resize=750%2C400&ssl=1)
Next, enter the application name, select its purpose and click Create App.
![facebookapp7 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 7](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_7-5652077.jpeg?resize=750%2C400&ssl=1)
Next enter your Facebook login password
![facebookapp8 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 8](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_8-1367043-5082367.jpg?resize=750%2C400&ssl=1)
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.
![facebookapp9 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 9](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_9-5392303-5865048.jpg?resize=750%2C400&ssl=1)
To get the token run Graph API Explorer.
![facebookapp10 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 10](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_10-5804984.jpeg?resize=750%2C400&ssl=1)
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).
![facebookapp11 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 11](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_11-3596776.jpeg?resize=750%2C400&ssl=1)
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.
![facebookapp12 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 12](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_12-7071310.jpeg?resize=750%2C400&ssl=1)
Next, click Continue
![facebookapp13 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 13](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_13-2876586-6143486.jpg?resize=574%2C725&ssl=1)
Select the page you need and save the ID for further posting:
![facebookapp14 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 14](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_14-7454480-1968314.jpg?resize=578%2C723&ssl=1)
Both options have to be turned on. In our case Softacom. Click Further.
![facebookapp15 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 15](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_15-1726484-8043323.jpg?resize=580%2C727&ssl=1)
![facebookapp16 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 16](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_16-6041488-4661737.jpg?resize=578%2C725&ssl=1)
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.
![facebookapp17 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 17](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_17-4561124-6065033.jpg?resize=750%2C210&ssl=1)
Select your page (Softacom)
![facebookapp18 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 18](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_18-9075864-1041071.jpg?resize=745%2C725&ssl=1)
Set up special permissions for posting (publish_to_groups, page_manage_posts).
![facebookapp19 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 19](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_19-3585011.jpeg?resize=750%2C400&ssl=1)
![facebookapp20 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 20](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_20-3028785.jpeg?resize=750%2C401&ssl=1)
Click “Generate Access Token” to generate an access token in order to save the permissions settings.
![facebookapp21 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 21](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_21-2778427.jpeg?resize=750%2C396&ssl=1)
![facebookapp22 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 22](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_22-9156416.png?resize=613%2C410&ssl=1)
Permissions are saved successfully and a new access token has been generated.
![facebookapp23 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 23](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_23-6779859.jpeg?resize=750%2C400&ssl=1)
Now select your page from the list
![facebookapp24 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 24](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_24-2884431-5982118.jpg?resize=745%2C725&ssl=1)
Send the GET request to get the page name and ID to ensure the Facebook Graph API works correctly.
![facebookapp25 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 25](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_25-6777693.jpeg?resize=750%2C398&ssl=1)
Send a test POST request to add a post to the page in form of a text message.
![facebookapp26 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 26](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_26-3948504.jpeg?resize=750%2C400&ssl=1)
Now refresh the page and you should see the text message posted on the page.
![facebookapp27 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 27](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_27-2904783-9152097.jpg?resize=750%2C400&ssl=1)
To delete a post, execute a DELETE request using the id of our post
![facebookapp28 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 28](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_28-8754913.jpeg?resize=750%2C400&ssl=1)
You should receive a response in JSON format telling that the post was deleted successfully.
![facebookapp29 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 29](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_29-4567420.jpeg?resize=750%2C160&ssl=1)
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.
![facebookapp30 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 30](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_30-3335370.jpeg?resize=750%2C193&ssl=1)
Next Open in Access Token Tool
![facebookapp31 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 31](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_31-6247790.jpeg?resize=750%2C400&ssl=1)
Choose Extend Access Token
![facebookapp32 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 32](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_32-9348416-2488836.jpg?resize=750%2C391&ssl=1)
![facebookapp33 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 33](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_33-8245309-2835621.jpg?resize=750%2C400&ssl=1)
Copy and save the resulting token
![facebookapp34 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 34](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_34-7275142-3736994.jpg?resize=750%2C251&ssl=1)
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
![facebookapp35 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 35](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_35-9706384-2342156.jpg?resize=750%2C399&ssl=1)
![facebookapp36 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 36](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_36-2084528-9042745.jpg?resize=750%2C400&ssl=1)
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.
![facebookapp37 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 37](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_37-8701722.png?resize=750%2C308&ssl=1)
To enter messages use the TMemo component.
![facebookapp38 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 38](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_38-3379999.png?resize=750%2C294&ssl=1)
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.
![facebookapp39 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 39](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_39-8253264.png?resize=750%2C338&ssl=1)
The response from the server in the form of JSON will be stored in the TMemo component.
![facebookapp40 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 40](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_40-9139252.png?resize=750%2C469&ssl=1)
In the onCreate form event handler, we assign the page access token to a variable of type string (token)
![facebookapp41 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 41](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_41-5058538.png?resize=750%2C142&ssl=1)
The text message entered by the user is stored in the “mes” string variable.
![facebookapp42 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 42](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_42-2193810.png?resize=502%2C60&ssl=1)
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.
![facebookapp43 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 43](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_43-7891677.png?resize=505%2C167&ssl=1)
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.
![facebookapp44 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 44](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_44-5664469.png?resize=713%2C112&ssl=1)
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
// Post simple message to Facebook Page procedure TForm1.btnPostMessageClick(Sender: TObject); begin mes := MessageMemo.Text; TTask.Run( procedure var LMultipartFormData: TMultipartFormData; LMS: TMemoryStream; begin LMultipartFormData := TMultipartFormData.Create; LMultipartFormData.AddField('message', mes); LMS := TMemoryStream.Create; NetHTTPRequest1.Post('https://graph.facebook.com/v12.0/103766905472273/feed?message&access_token=' + token, LMultipartFormData, LMS); TThread.Synchronize(nil, procedure begin ResponseMemo.Lines.LoadFromStream(LMS); TabControl1.GotoVisibleTab(1) end); LMS.Free; LMultipartFormData.Free; end); end; // Post url picture to Facebook Page procedure TForm1.btnPostPictureClick(Sender: TObject); begin TTask.Run( procedure var LMultipartFormData: TMultipartFormData; LMS: TMemoryStream; begin LMultipartFormData := TMultipartFormData.Create; LMultipartFormData.AddField('url', EditURLPicture.Text); LMS := TMemoryStream.Create; NetHTTPRequest1.Post('https://graph.facebook.com/v12.0/103766905472273/photos?url&access_token=' + token, LMultipartFormData, LMS); TThread.Synchronize(nil, procedure begin ResponseMemo.Lines.LoadFromStream(LMS); TabControl1.GotoVisibleTab(1) end); LMS.Free; LMultipartFormData.Free; end); end; // Delete post by Id procedure TForm1.btnDeleteClick(Sender: TObject); begin TTask.Run( procedure var LMS: TMemoryStream; begin LMS := TMemoryStream.Create; NetHTTPRequest1.Delete('https://graph.facebook.com/v12.0/' + Edit2.Text + '?access_token=' + token, LMS); TThread.Synchronize(nil, procedure begin ResponseMemo.Lines.LoadFromStream(LMS); TabControl1.GotoVisibleTab(1) end); LMS.Free; end); end; // Post Message and link to Facebook Page procedure TForm1.btnUpdateClick(Sender: TObject); begin mes := MessageMemo.Text; TTask.Run( procedure var LMultipartFormData: TMultipartFormData; LMS: TMemoryStream; begin LMultipartFormData := TMultipartFormData.Create; LMultipartFormData.AddField('message', mes); LMS := TMemoryStream.Create; NetHTTPRequest1.Post('https://graph.facebook.com/v12.0/' + Edit2.Text + '?message&access_token=' + token, LMultipartFormData, LMS); TThread.Synchronize(nil, procedure begin ResponseMemo.Lines.LoadFromStream(LMS); TabControl1.GotoVisibleTab(1) end); LMS.Free; LMultipartFormData.Free; end); end; procedure TForm1.btnMessageLinkPostClick(Sender: TObject); begin mes := MessageMemo.Text; link := Edit3.Text; TTask.Run( procedure var LMultipartFormData: TMultipartFormData; LMS: TMemoryStream; begin LMultipartFormData := TMultipartFormData.Create; LMultipartFormData.AddField('message', mes); LMultipartFormData.AddField('link', link); LMS := TMemoryStream.Create; NetHTTPRequest1.Post ('https://graph.facebook.com/v12.0/103766905472273/feed?message&link&access_token=' + token, LMultipartFormData, LMS); TThread.Synchronize(nil, procedure begin ResponseMemo.Lines.LoadFromStream(LMS); TabControl1.GotoVisibleTab(1) end); LMS.Free; LMultipartFormData.Free; end); end; |
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
![facebookapp45 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 45](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_45-1707990.png?resize=542%2C589&ssl=1)
![facebookapp46 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 46](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_46-7174113-6083501.jpg?resize=542%2C589&ssl=1)
Below is the result of posting a text message on the Softacom page
![facebookapp47 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 47](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_47-7064758-6526883.jpg?resize=750%2C400&ssl=1)
![facebookapp48 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 48](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_48-6452559.png?resize=542%2C645&ssl=1)
![facebookapp49 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 49](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_49-4917161-3990575.jpg?resize=542%2C645&ssl=1)
The result of posting a text message and a link on the Softacom page
![facebookapp50 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 50](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_50-8252351-2888029.jpg?resize=750%2C400&ssl=1)
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.
![facebookapp51 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 51](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_51-3868016.png?resize=536%2C231&ssl=1)
Enter Id and click “Delete post by id”.
![facebookapp52 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 52](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_52-6186730.png?resize=552%2C645&ssl=1)
The post on the Softacom page has been successfully deleted.
![facebookapp53 | Embarcadero RAD Studio Delphi C++Builder Blogs facebook app 53](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/01/facebook_app_53-9364652-9619804.jpg?resize=542%2C219&ssl=1)
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?
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition