![smartmockupslasj5pbu | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/smartmockups_lasj5pbu-9300280.jpg?resize=1140%2C694&ssl=1)
![How to receive Facebook account info via an Embarcadero Delphi FMX app | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App Softacom header](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/How-to-receive-Facebook-account-info-via-an-Embarcadero-Delphi-FMX-app-2669855.jpg?resize=300%2C195&ssl=1)
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
.
![19603974 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App Facebook app creation page](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/1-9603974.png?resize=750%2C377&ssl=1)
Then we need to choose the Consumer app type.
![27199212 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App an app type selection page](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/2-7199212.png?resize=750%2C318&ssl=1)
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.
![31283387 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App confirm the app type](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/3-1283387.png?resize=750%2C128&ssl=1)
Let’s enter a name for our app and press Create App.
![42035505 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App providing basic information for the Facebook app](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/4-2035505.png?resize=750%2C349&ssl=1)
The app has been created.
![5559154239 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App main page of our Facebook app](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/555-9154239.png?resize=750%2C383&ssl=1)
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.
![6663004683 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App selection graph API Explorer tool](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/666-3004683.png?resize=750%2C305&ssl=1)
For working with the Facebook API, we need to get a user’s token. To do that we need to choose Get Token.
![7771775156 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App app's access token selection](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/777-1775156.png?resize=750%2C380&ssl=1)
We should choose Get User Access Token.
![8885461159 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App getting the user access token](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/888-5461159.png?resize=750%2C375&ssl=1)
Now just click Continue as a current user.
![456456 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/456456-2949777.jpg?resize=750%2C514&ssl=1)
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.
![1010102130731 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App special permissions selection](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/101010-2130731.png?resize=750%2C385&ssl=1)
Let’s press Add Permissions and then User Data Permissions.
![1111115232173 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App user data permissions](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/111111-5232173.png?resize=750%2C383&ssl=1)
In our example, we will choose all the permissions offered on the dropdown list.
![1212124550642 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App choosing all necessary permissions](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/121212-4550642.png?resize=750%2C419&ssl=1)
For using all the necessary permissions, we need to click the Generate Access Token button.
![1313131464683 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App access token generation](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/131313-1464683.png?resize=670%2C624&ssl=1)
Now just press Continue as the current user.
![22222 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/22222-3983678.jpg?resize=750%2C478&ssl=1)
Let’s make a test GET request for getting a user’s name and id. We need just to click Submit.
![1515158443424 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App test GET request implementation](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/151515-8443424.png?resize=750%2C385&ssl=1)
The ID and user’s name have been successfully received in the response in JSON format.
![1616169199734 | Embarcadero RAD Studio Delphi C++Builder Blogs 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)](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/161616-9199734.png?resize=750%2C184&ssl=1)
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.
![1717171147994 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App GET request with all necessary parameters user email user name user birthday user likes etc](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/171717-1147994.png?resize=750%2C384&ssl=1)
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.
![1818186043142 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App access token for further using in our Delphi FireMonkey FMX app](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/181818-6043142.png?resize=750%2C389&ssl=1)
For confirming our choice, we should re-enter our password and press Submit.
![1919199820317 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App user's password confirmation](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/191919-9820317.png?resize=750%2C380&ssl=1)
The received long-term token should be saved for further use in our FMX Delphi app.
![2020208800247 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App our long term access token](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/202020-8800247.png?resize=750%2C379&ssl=1)
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.
![2121214112234 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App TNetHTTPClient component on the Net tab](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/212121-4112234.png?resize=750%2C404&ssl=1)
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).
![2222224366495 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App TMemo component on the Standard tab](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/222222-4366495.png?resize=750%2C404&ssl=1)
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).
![2323238414536 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App all private fields of our class](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/232323-8414536.png?resize=750%2C310&ssl=1)
This class also has a constructor (Create) and two methods (ConnectToAPI, FormatJSON).
![2424245079545 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App all methods of our class](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/242424-5079545.png?resize=750%2C157&ssl=1)
The constructor allows conducting setting of all the necessary fields when you are creating an instance of a class (object).
![2525255542944 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App the constructor of our class](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/252525-5542944.png?resize=750%2C291&ssl=1)
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.
![2626264644641 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App ConnectTAPI method](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/262626-4644641.png?resize=750%2C249&ssl=1)
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.
![2727278026824 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App TNetHTTPClient's GET method](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/272727-8026824.png?resize=750%2C67&ssl=1)
The program code of the TFacebookAPIClass class is presented 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 |
unit TFacebookAPIClass interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Memo.Types, FMX.ScrollBox, FMX.Memo, FMX.StdCtrls, FMX.Controls.Presentation, System.Net.URLClient, System.Net.HttpClient, System.Net.HttpClientComponent, JSON, System.Threading; type TFacebookAPI = class private NetHttpClient: TNetHTTPClient; ApiCommand: string; AccessToken: string; Memo: TMemo; public constructor Create(NetHttpClient: TNetHTTPClient; ApiCommand, AccessToken: string; Memo: TMemo); overload; procedure ConnectToAPI; overload; function FormatJSON(JSON: String): string; overload; end; implementation { TFacebookAPI } constructor TFacebookAPI.Create(NetHttpClient: TNetHTTPClient; ApiCommand, AccessToken: string; Memo: TMemo); begin if NetHttpClient is TNetHTTPClient then Self.NetHttpClient := NetHttpClient else begin ShowMessage('Wrong class instance!'); Exit; end; if AccessToken <> '' then Self.AccessToken := AccessToken else begin ShowMessage('Access token is empty!'); Exit; end; if ApiCommand <> '' then Self.ApiCommand := ApiCommand else begin ShowMessage('ApiCommand is empty!'); Exit; end; if Memo is TMemo then Self.Memo := Memo else begin ShowMessage('Wrong class instance!'); Exit; end; end; procedure TFacebookAPI.ConnectToAPI; begin TTask.Run( procedure var LResponse: TStringStream; begin try LResponse := TStringStream.Create; NetHttpClient.Get(ApiCommand + AccessToken, LResponse); TThread.Synchronize(nil, procedure begin Memo.Text := FormatJSON(LResponse.DataString); end); finally FreeAndNil(LResponse); Self.Free; end; end); end; function TFacebookAPI.FormatJSON(JSON: string): string; var tmpJson: TJsonObject; begin tmpJson := TJsonObject.ParseJSONValue(JSON) as TJsonObject; try Result := tmpJson.Format(); finally FreeAndNil(tmpJson); end; end; end. |
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.
![2828289357880 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App the button's OnClick handler](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/282828-9357880.png?resize=750%2C305&ssl=1)
Let’s test our FMX Delphi app. The result of the app work is presented below.
![2929299812694 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App our Delphi FireMonkey FMX app running on a Windows platform](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/292929-9812694.png?resize=750%2C590&ssl=1)
Here you can see the received response in JSON format in accordance with the real data of the Facebook account.
![3030301362399 | Embarcadero RAD Studio Delphi C++Builder Blogs Read Facebook Account Data In A Cross Platform App Comparison of our real Facebook user accounts data to obtained in JSON format from our Delphi FMX app](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2022/11/303030-1362399.png?resize=750%2C337&ssl=1)
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.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition