Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Gordon Saxby 1461 posts 1883 karma points
    May 24, 2016 @ 19:26
    Gordon Saxby
    0

    Not sure how to get started

    I want to display the latest few (5 - 10?) posts from a Facebook page that I manage for a local club, on their website.

    I played with Facebook's Graph Api Explorer and came up with

    pagename/feed?fields=created_time,message,id,picture&limit=5
    

    which gives me the basic data I need.

    Is Skybrud the right tool for this job? I don't need to do any updates, or anything more fancy than just get the latest few posts. There will be links to encourage visitors to go to Facebook to see all posts.

    I generated a Page Access Token using the Graph Api Explorer - but how do I do that in code? Or is this something that needs to be setup in Facebook?

    I get the feeling that this is probably very simple, but I can't get my head around it at the moment!

  • Anders Bjerner 487 posts 2990 karma points MVP 8x admin c-trib
    May 24, 2016 @ 19:43
    Anders Bjerner
    0

    Hi Gordon,

    Since you write posts, wouldn't in more be like the posts endpoint rather than feed?

    pagename/posts?fields=created_time,message,id,picture&limit=5
    

    If so, you can use the following code to fetch the 10 most recent posts of your Facebook page:

    @using Skybrud.Social.Facebook
    @using Skybrud.Social.Facebook.Objects.Posts
    @using Skybrud.Social.Facebook.Options.Posts
    @using Skybrud.Social.Facebook.Responses.Posts
    
    @{
    
        // Initialize a new service from an access token (user, page or app)
        FacebookService service = FacebookService.CreateFromAccessToken("your access token");
    
        // Make the request to the Facebook Graph API
        FacebookPostsResponse response = service.Posts.GetPosts(new FacebookGetPostsOptions {
            Identifier = "umbraco",
            Fields = "created_time,message,id,picture",
            Limit = 10
        });
    
        // Iterate through each post
        foreach (FacebookPost post in response.Body.Data) {
    
            // do something with the post
    
        }
    
    }
    

    I can't remember exactly how page tokens work, and whether they expire (I think they might). But you can use what is called an app access token as long as the posts you wish to fetch is public. If you already have created a Facebook app, you can find the app access token on this page:

    https://developers.facebook.com/tools/accesstoken/

  • Gordon Saxby 1461 posts 1883 karma points
    May 24, 2016 @ 20:46
    Gordon Saxby
    0

    See, I said I didn't know where to start!

    "If you already have created a Facebook app" - umm, I have created something, let's hope it's the right thing :-)

    Thanks very much for the sample code.

Please Sign in or register to post replies

Write your reply to:

Draft