HowTo: Request data from Facebook using Graph API and Access Token

Official Content
This documentation is valid for:

This document presents an example of how to use Facebook's GraphAPI in Smart Device applications using GeneXus. 

Step 1: Register your application

You must register your application on the Facebook developer site and get your Facebook App Id.
Refer to HowTo: Register a Facebook App for Smart Device interaction for detailed information.

Step 2: Integrate Facebook's login and request access token

Use SD Facebook Button control to allow the end user to log in to the GeneXus app using Facebook's credentials. This action will allow you to get the user's Access Token and use the Graph API for retrieving user data.

For example, you can use the OnUserInfoUpdated event and request the access token as follows:

Event &SDFacebookButton.OnUserInfoUpdated
    &FacebookAccessToken = Facebook.AccessToken

where &FBAccessToken is based on FacebookAccessToken SDT.

In this case, we will use the Graph API for getting the user's posts. To do so, the developer must ensure that "user_posts" tag is added to Read Permission property. When the end user logs in, Facebook will inform him/her about your purpose (in this case, get his/her posts).

Step 3: Request data from Facebook using Graph API

Write an online Procedure for requesting the GraphAPI data by using HttpClient data type. This procedure should receive in its parm rule the access token and user identifier strings obtained from the device.

For example, suppose the procedure is called GetUserPosts, and it has defined the following parm rule:

parm(in:&AccessToken, in:&UserId, out:&FBJson);

You can write a source code as follows:

&FBVersion = !"v2.12" // Change it if it is necessary
&Url = !""
&Url = &Url.Replace("%version",&FBVersion)
&Url = &Url.Replace("%user_id",&UserId)
&Url = &Url.Replace("%access_token",&AccessToken)
&FBJson = &HttpClient.ToString()

Then, in a user event, you can invoke this procedure as follows:

Event 'Get My Posts'
    &FBJson = GetUserPosts(&FacebookAccessToken.AccessToken, &FacebookAccessToken.UserId)

As a result, after the user has logged in, the &FBJson string-based variable will have the following value:

   "data": [
         "story": "Damian Salvia is with Pablo Martinez and 5 others.",
         "created_time": "2018-05-07T16:13:21+0000",
         "id": "<id>"
         "story": "Stella Marys Pereira Larrosa added a new photo \u2014 with Nicolas Mechulam and 2 others.",
         "created_time": "2019-07-11T15:12:25+0000",
         "id": "<id>"
   "paging": {
      "previous": "<user_id>/feed?since=<datetime>&access_token=<token>&limit=<number>&__paging_token=<token>&__previous=1",
      "next": "<user_id>/feed?access_token=<token>&limit=<number>&until=<datetime>&__paging_token=<token>"

It's your responsibility to parse the result to meet your requirements.


Full Graph API integration is available as of Genexus 15 Upgrade 10.

See also