Interact with Facebook APIs using GAM

Unofficial Content

Using Additional Scope Property for GAM Google / Facebook Authentication Types you can interact with Facebook API from the application which has integrated security enabled (GeneXus Access Manager).

Example

The following is an example where we get the news in Facebook site for the user who is logged in.

1. Define GAM Facebook Authentication Type (X Evolution 3 upgrade 3 and prior).

FacebookAdditionalScope

Specify Additional Scope = read_stream as seen in the figure above.

The code used for setting the Additional scope for Facebook Authentication is the following (see GAMExampleEntryAuthenticationType webpanel, which is part of the GAM samples distributed).

&AuthenticationTypeFacebook.Facebook.AdditionalScope= &AdditionalScope

Note that AuthenticationTypeFacebook is a variable based on GAMAuthenticationTypeFacebook external object. See the figure below for details on this data type.

GAMAuthenticationTypeFacebook

2. Next we show the sample code to interact with the Facebook API and get the news in Facebook site for the user in the GAM session.

First we get the current GAMSession and we get the ExternalToken of it by means of the ExternalToken GAMSession property.
Afterwards we can define a variable based on HTTPClient data type in order to execute a method of the Facebook API which receives the ExternalToken obtained previously.

&isSessionValid = GAMSession.IsValid(&GAMSession, &GAMErrors) //&GAMSession is GAMSession data type, &GAMErrors is collection of GAM Error data type
&UserName = &GAMSession.User.Name
&Authentication = &GAMSession.User.AuthenticationTypeName

If &isSessionValid and not &GAMSession.IsAnonymous
  If &GAMSession.User.AuthenticationTypeName = !'facebook'
   //Read facebook news
   &httpClient.Host = !"graph.facebook.com"
   &httpClient.BaseUrl = !"/"
   &HttpClient.Secure = 1
   &HttpClient.Port = 443
   &StrCall = !"me/home?access_token=" + &GAMSession.ExternalToken.Trim()
   &httpClient.Execute("GET", &StrCall)
   &ResultHttp = &httpClient.ToString()
   If &httpClient.StatusCode = 200
    &data.fromJson(&ResultHttp)//&data is FacebookFeed data type, and &data2 is FacebookFeedItem data type.
    &data2 = &data.data
   Else
    &FacebookError.FromJson(&ResultHttp)
    msg(!"StatusCode:"+&httpClient.StatusCode.Truncate(0).ToString()+ !" - " + &FacebookError.error.type + !": " + &FacebookError.error.message )
   Endif
  Else
   msg('User not valid.')
  Endif
Else
  msg('Sesion not valid.')
  Do 'ProcessErrors'
Endif

Then we have to process the data retrieved from Facebook. In this example we have a grid named "GridNews" where we load the data.

Event Load

   //Loop over the data 
   For &dataitem in &data2 //&dataitem is FacebookFeedItem.FacebookFeedItemItem data type
   Do case
  Case &dataitem.type = 'photo'
   do 'load_photo'
   GridNews.Load()
  Case &dataitem.type = 'status'
   do 'load_status'
   GridNews.Load()
  Case &dataitem.type = 'question'
   do 'load_question'
   GridNews.Load()
  Case &dataitem.type = 'link'
   do 'load_link'
   GridNews.Load()
  Case &dataitem.type = 'video'
   do 'load_video'
   GridNews.Load()
  Endcase
    Endfor
Endevent


Sub 'Load_photo'
&Image.FromURL(&dataitem.picture)
&Date = &dataitem.created_time
&Message = &dataitem.description
EndSub

Sub 'Load_status'
&Image.FromURL("")
&Date = &dataitem.created_time
&Message = &dataitem.message
EndSub

Sub 'Load_question'
&Image.FromURL("")
&Date = &dataitem.created_time
&Message = &dataitem.story
EndSub

Sub 'Load_link'
&Image.FromURL(&dataitem.picture)
&Date = &dataitem.created_time
&Message = &dataitem.name + ' ' + &dataitem.link
EndSub

Sub 'Load_video'
&Image.FromURL(&dataitem.picture)
&Image.Link = &dataitem.source
&Date = &dataitem.created_time
&Message = &dataitem.description
EndSub

Sub 'ProcessErrors'
    For &GAMError in &GAMErrors
        If &GAMError.Code <> GAMErrorMessages.SessionExpired
            Msg(Format("%1 (GAM%2)", &GAMError.Message, &GAMError.Code))
        Endif
    EndF

 

The data types used in this example are like the following:

FacebookdataSample

FacebookdataSample2

Note:

  • The Additional Scope can be any string, and you can specify more than one separated by "+".
  • There are other available scopes, here.

See Also

Interact with Google APIs using GAM