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

Read Facebook Account Data In A Cross Platform App

Read Facebook Account Data In A Cross Platform App
Read Facebook Account Data In A Cross Platform App - Softacom header

Facebook is a social network that boasts high popularity among users from all over the world. Earlier, we have already shown how it is possible to automate the process of posting different types of info on a Facebook page using the Facebook Graph API. We’ve created a Facebook app, an access token and API commands that we’ve further used in our cross platform apps using Delphi and the FireMonkey FMX framework.

In this article, we will show how to get Facebook account info (a user’s name, number of friends, date of birth, information about posts, likes, etc.) without signing into an account. For getting an access token, choosing the necessary settings, and performing some other preparatory actions for the first time, we need to sign in. For further work with the Facebook API, using our FMX Delphi cross platform app, we need to create and set up a special Facebook ‘app’. This Facebook ‘app’ will allow us to get information about a Facebook user, using the API. For doing that we need to open the developer console by following the link https://developers.facebook.com. Then we need to choose the My Apps tab and click Create App.

Read Facebook Account Data In A Cross Platform App - Facebook app creation page

Then we need to choose the Consumer app type.

Read Facebook Account Data In A Cross Platform App - an app type selection page

A Consumer app will allow us to get information about a Facebook user (a user’s name, date of birth, email, etc.). A Company app is suitable for such tasks as posting on a Facebook page. In our case, we use a Consumer app for having the possibility to get information about a Facebook user. For creating this app, we need to press Next.

Read Facebook Account Data In A Cross Platform App - confirm the app type

Let’s enter a name for our app and press Create App.

Read Facebook Account Data In A Cross Platform App - providing basic information for the Facebook app

The app has been created.

Read Facebook Account Data In A Cross Platform App - main page of our Facebook app

For testing app functioning, getting a user’s token and making test GET requests for receiving information about a current user in the JSON format, let’s use Graph API Explorer. Now we should choose Tools and then Graph API Explorer.

Read Facebook Account Data In A Cross Platform App - selection graph API Explorer tool

For working with the Facebook API, we need to get a user’s token. To do that we need to choose Get Token.

Read Facebook Account Data In A Cross Platform App - app's access token selection

We should choose Get User Access Token.

Read Facebook Account Data In A Cross Platform App - getting the user access token

Now just click Continue as a current user.

Read Facebook Account Data In A Cross Platform App - access to current user account confirmation

For getting various information about the account (a user’s name, number of friends, date of birth, information about posts and likes, etc.) we should set the relevant permissions.

Read Facebook Account Data In A Cross Platform App - special permissions selection

Let’s press Add Permissions and then User Data Permissions.

Read Facebook Account Data In A Cross Platform App - user data permissions

In our example, we will choose all the permissions offered on the dropdown list.

Read Facebook Account Data In A Cross Platform App - choosing all necessary permissions

For using all the necessary permissions, we need to click the Generate Access Token button.

Read Facebook Account Data In A Cross Platform App access token generation

Now just press Continue as the current user.

Read Facebook Account Data In A Cross Platform App - access to current user account confirmation

Let’s make a test GET request for getting a user’s name and id. We need just to click Submit.

Read Facebook Account Data In A Cross Platform App - test GET request implementation

The ID and user’s name have been successfully received in the response in JSON format.

Read Facebook Account Data In A Cross Platform App - the result of our GET request (we received the user's ID and first name from the Facebook graph API)

Let’s make a test GET request with a bigger number of parameters. As a response, we will get information about the date of birth, the number of friends, URL link to the profile photo, information about the user’s posts, etc. And all this will be provided in JSON format.

Read Facebook Account Data In A Cross Platform App - GET request with all necessary parameters (user email, user name, user birthday, user likes etc)

For getting information about a user with the help of an FMX Embarcadero Delphi app, it is necessary to get a long-term token. Now we need just to press “i” and then click Open in Access Token Tool.

Read Facebook Account Data In A Cross Platform App - access token for further using in our Delphi FireMonkey FMX app

For confirming our choice, we should re-enter our password and press Submit.

Read Facebook Account Data In A Cross Platform App - user's password confirmation

The received long-term token should be saved for further use in our FMX Delphi app.

Read Facebook Account Data In A Cross Platform App - our long-term access token

For making a GET request with further receiving data about a Facebook user in JSON format in our Delphi app, we will use a standard component TNetHttpClient.

Read Facebook Account Data In A Cross Platform App - TNetHTTPClient component (on the Net tab)

For showing the response from the Facebook Graph API with the information about a user in JSON format, let’s use a TMemo component (tab Standard on the Component Palette).

Read Facebook Account Data In A Cross Platform App - TMemo component (on the Standard tab)

For our convenient work with the Facebook Graph API, we will use a class named TFacebookAPIClass. This class has four private fields. The current client of the class TNetHttpClient (NetHttpClient), the URL address of the  Facebook Graph API (ApiCommand), a user access token (AccessToken), and a multi-line input field TMemo for saving a JSON reply from the server (Memo).

Read Facebook Account Data In A Cross Platform App - all private fields of our class

This class also has a constructor (Create) and two methods (ConnectToAPI, FormatJSON).

Read Facebook Account Data In A Cross Platform App - all methods of our class

The constructor allows conducting setting of all the necessary fields when you are creating an instance of a class (object).

Read Facebook Account Data In A Cross Platform App - the constructor of our class

In the ConnectToAPI method, a GET inquiry is executed using the Get method of the TNetHttpClient class. The LResponse object of the TStringStream class is used for storing a JSON response received from the server. For formatting a JSON response from the server with its further demonstration in TMemo, the FormatJSON method is used.

Read Facebook Account Data In A Cross Platform App - ConnectTAPI method

As the first entry parameter, the Get method takes an API command for getting access to the Facebook Graph API and a user access token. As the second parameter, a variable LResponse of the TStringStream is taken.

Read Facebook Account Data In A Cross Platform App - TNetHTTPClient's GET method

The program code of the TFacebookAPIClass class is presented below.

The click processor of the button “Request to Facebook API” contains the logic of the app’s work for interaction with the Facebook API. Here we need to create an object FacebookAPI of the TFacebookAPI class. Now we need to call a constructor with further transfer of all the necessary parameters (an object of the TNetHttpClient, Facebook API URL, user access token,  object of the TMemo class). The entire work related to the connection to the FacebookAPI with further getting a JSON response is executed by the ConnectToAPI method.

Read Facebook Account Data In A Cross Platform App - the button's OnClick handler

Let’s test our FMX Delphi app. The result of the app work is presented below.

Read Facebook Account Data In A Cross Platform App - our Delphi FireMonkey FMX app running on a Windows platform

Here you can see the received response in JSON format in accordance with the real data of the Facebook account.

Read Facebook Account Data In A Cross Platform App - Comparison of our real Facebook user account’s data to obtained in JSON format from our Delphi FMX app

This article was written by Embarcadero Tech Partner Softacom. Softacom specialize in all sorts of software development focused on Delphi. Read more about their services on the Softacom website.


What's Coming in Delphi, C++Builder, and RAD Studio 11.2 Alexandria

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

Leave a Reply

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

IN THE ARTICLES