Facebook Getting a post

Getting a post

If you already know the ID of a given Facebook post, you can use the GetPost method to get further information about the post - this can be done like:

@using Skybrud.Social.Facebook
@using Skybrud.Social.Facebook.Models.Posts
@using Skybrud.Social.Facebook.Responses.Posts
@inherits WebViewPage<FacebookService>
              
@{

    // Make the request to the API
    FacebookGetPostResponse response = Model.Posts.GetPost("185238545407_10158539914495408");

    // Get a reference to the post
    FacebookPost post = response.Body;

    <p>ID: @post.Id</p>
    <p>Message: @post.Message</p>
    <p>Story: @post.Story</p>
    <p>Created: @post.CreatedTime</p>

}

Fields

Like the rest of the Graph API, Facebook will only respond with a number of fields by default. For a post, this will be the created_time, message, story and id fields.

To request more fields, you can pass a second parameter to the GetPost method with the type of either FacebookFieldsCollection or FacebookField:

@using Skybrud.Social.Facebook
@using Skybrud.Social.Facebook.Constants
@using Skybrud.Social.Facebook.Fields
@using Skybrud.Social.Facebook.Models.Posts
@using Skybrud.Social.Facebook.Responses.Posts
@inherits WebViewPage<FacebookService>
              
@{
    
    // Initialize a collection of the fields that Facebook should return
    FacebookFieldsCollection fields = new FacebookFieldsCollection(
        FacebookPostFields.Id,
        FacebookPostFields.Message,
        FacebookPostFields.Story,
        FacebookPostFields.CreatedTime,
        FacebookPostFields.From
    );

    // Make the request to the API
    FacebookGetPostResponse response = Model.Posts.GetPost("185238545407_10158539914495408", fields);

    // Get a reference to the post
    FacebookPost post = response.Body;

    <p>ID: @post.Id</p>
    <p>Message: @post.Message</p>
    <p>Story: @post.Story</p>
    <p>Created: @post.CreatedTime</p>
    <p>From: @post.From.Name</p>
    
}

Since we're now specifically requesting the from field, we can then also read the author of the post via the post.From property, as it now has a value in the underlying JSON.