When using Additional Scope Property for GAM Google / Facebook Authentication Types you can interact with Facebook API from the application which has GAM activated.
The following is an example where to get the news in Facebook site for the user who is logged in.
1. Define GAM Facebook Authentication Type.
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.
2. Next, you can see the sample code to interact with the Facebook API and get the news in Facebook site for the user in the GAM session.
First, you get the current GAMSession and get the ExternalToken of it by means of the ExternalToken GAMSession property.
Afterwards you 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 you have to process the data retrieved from Facebook. In this example, you have a Grid named "GridNews" where you 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
EndFor
EndSub
The data types used in this example are like the following:
- The Additional Scope can be any string, and you can specify more than one separated by "+".
- There are other available scopes, here.
Interact with Google APIs using GAM