Sample code for 30+ languages & platforms
DataFlex

Read a Single Facebook Post

See more Facebook Examples

Demonstrates how to read the contents of a single Facebook post. A post is an individual entry in a profile's feed. The profile could be a user, page, app, or group.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vOauth2
    Handle hoOauth2
    Handle hoRest
    String sPostId
    Handle hoSbPath
    String sResponseJson
    Handle hoJson
    Handle hoDtime
    Boolean iBLocalTime
    Variant vDt
    Handle hoDt
    String sTemp1
    Integer iTemp1
    Integer iTemp2
    Integer iTemp3
    Integer iTemp4
    Integer iTemp5
    Boolean bTemp1

    Move False To iSuccess

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // This example assumes a previously obtained an access token
    Get Create (RefClass(cComChilkatOAuth2)) To hoOauth2
    If (Not(IsComObjectCreated(hoOauth2))) Begin
        Send CreateComObject of hoOauth2
    End
    Set ComAccessToken Of hoOauth2 To "FACEBOOK-ACCESS-TOKEN"

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End

    // Connect to Facebook...
    Get ComConnect Of hoRest "graph.facebook.com" 443 True True To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Provide the authentication credentials (i.e. the access key)
    Get pvComObject of hoOauth2 to vOauth2
    Get ComSetAuthOAuth2 Of hoRest vOauth2 To iSuccess

    // This example assumes a post id was already retrieved.
    // For example, it could've been retrieved by reading the user's feed:
    // See Parsing the Facebook User Feed for code showing how to parse the JSON feed content.

    Move "10224048320139890_10210156138515282" To sPostId

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbPath
    If (Not(IsComObjectCreated(hoSbPath))) Begin
        Send CreateComObject of hoSbPath
    End
    Get ComAppend Of hoSbPath "/v2.7/" To iSuccess
    Get ComAppend Of hoSbPath sPostId To iSuccess

    // Select the fields we want.
    // This example will select almost all the possible fields.
    // See https://developers.facebook.com/docs/graph-api/reference/post/
    Get ComAddQueryParam Of hoRest "fields" "id,message,created_time,caption,description,from,link,name,object_id,picture,place,privacy,properties,shares,source,status_type,story,targeting,to,type,updated_time,with_tags" To iSuccess

    Get ComGetAsString Of hoSbPath To sTemp1
    Get ComFullRequestNoBody Of hoRest "GET" sTemp1 To sResponseJson
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Set ComEmitCompact Of hoJson To False
    Get ComLoad Of hoJson sResponseJson To iSuccess

    // Show the JSON in human-readable format.
    Get ComEmit Of hoJson To sTemp1
    Showln sTemp1

    // A sample JSON response is shown here.  
    // { 
    //   "id": "12345678901234567_12345678900000004",
    //   "message": "Ignore my posts -- I'm doing some testing for Facebook related programming...",
    //   "created_time": "2016-09-29T20:46:18+0000",
    //   "from": { 
    //     "name": "John Doe",
    //     "id": "12345678901234567"
    //   },
    //   "link": "https:\/\/www.facebook.com\/photo.php?fbid=10210199026247451&set=a.1237223526054.2038240.1094202869&type=3",
    //   "object_id": "10210139026347451",
    //   "picture": "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-9\/14462791_10210199026647451_7830642117574407060_n.jpg?oh=a7f9ed10ce9cd81a82adeb541c60e2e2&oe=58ABB195",
    //   "privacy": { 
    //     "allow": "",
    //     "deny": "",
    //     "description": "Public",
    //     "friends": "",
    //     "value": "EVERYONE"
    //   },
    //   "status_type": "added_photos",
    //   "type": "photo",
    //   "updated_time": "2016-09-29T20:46:18+0000"
    // }

    // This is the code to parse some fields in the JSON response.
    Get ComStringOf Of hoJson "type" To sTemp1
    Showln "type: " sTemp1
    Get ComStringOf Of hoJson "message" To sTemp1
    Showln "message: " sTemp1
    Get ComStringOf Of hoJson "id" To sTemp1
    Showln "id: " sTemp1
    Get ComStringOf Of hoJson "link" To sTemp1
    Showln "link: " sTemp1
    Get ComStringOf Of hoJson "privacy.description" To sTemp1
    Showln "privacy descripton: " sTemp1

    Get Create (RefClass(cComCkDateTime)) To hoDtime
    If (Not(IsComObjectCreated(hoDtime))) Begin
        Send CreateComObject of hoDtime
    End
    Move True To iBLocalTime
    Get ComStringOf Of hoJson "created_time" To sTemp1
    Get ComSetFromTimestamp Of hoDtime sTemp1 To iSuccess
    Get Create (RefClass(cComChilkatDtObj)) To hoDt
    If (Not(IsComObjectCreated(hoDt))) Begin
        Send CreateComObject of hoDt
    End
    Get pvComObject of hoDt to vDt
    Send ComToDtObj To hoDtime iBLocalTime vDt

    Get ComMonth Of hoDt To iTemp1
    Get ComDay Of hoDt To iTemp2
    Get ComYear Of hoDt To iTemp3
    Get ComHour Of hoDt To iTemp4
    Get ComMinute Of hoDt To iTemp5
    Showln iTemp1 "/" iTemp2 "/" iTemp3 "  " iTemp4 ":" iTemp5


End_Procedure