Search code examples
facebookspringfacebook-graph-apispring-socialspring-social-facebook

Facebook page feed without user login


Maybe you can help me out with something. I want to fetch all publicly available posts on a Facebook page using Spring Social.

I don't need OAuth authentication with a certain user or something since I don't want to post, comment or whatsoever in the name of a somebody.

Still, Facebook requires to pass an access token when doing a request. So I am doing the following to fetch all public posts.

Now my question is: is there an easier, concise way to do this or does Spring Social only support the complete user authentication OAuth dance and not this rather simple way?

public List<Post> fetchFacebookPosts() {

    String accessTokenUrlString = String.format("https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=%s&client_secret=%s&redirect_url=%s", getFacebookAppId(), getFacebookAppSecret(), "http://www.return.url");

    String token = "";

    try {
        // HTTPURLConnection stuff omitted
        // returns: accessToken=xxxx|xxxxxxxxxxxxx

        token = response.toString().substring(response.toString().indexOf("=")+1);

    } catch (IOException e) {
        // ...
    }

    FacebookTemplate facebookTemplate = new FacebookTemplate(token);
    return facebookTemplate.feedOperations().getFeed("12345678");
}

Solution

  • For getting the feed of a Facebook Page, you can even use the most basic Access Token, the App Access Token, without User Login/Authorization.

    String appAccessToken = APP-ID + '|' + APP_SECRET;
    

    Read more about the different Access Tokens in those articles: