Get public page statuses using Facebook Graph API without Access Token

后端 未结 4 2030
余生分开走
余生分开走 2020-11-29 17:23

I\'m trying to use the Facebook Graph API to get the latest status from a public page, let\'s say http://www.facebook.com/microsoft

According to http://developers.fa

4条回答
  •  孤街浪徒
    2020-11-29 17:45

    You can get the posts by simply requesting the site that your browser would request and then extracting the posts from the HTML.

    In NodeJS you can do it like this:

    // npm i request cheerio request-promise-native
    const rp = require('request-promise-native'); // requires installation of `request`
    const cheerio = require('cheerio');
    
    function GetFbPosts(pageUrl) {
        const requestOptions = {
            url: pageUrl,
            headers: {
                'User-Agent': 'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0'
            }
        };
        return rp.get(requestOptions).then( postsHtml => {
            const $ = cheerio.load(postsHtml);
            const timeLinePostEls = $('.userContent').map((i,el)=>$(el)).get();
            const posts = timeLinePostEls.map(post=>{
                return {
                    message: post.html(),
                    created_at: post.parents('.userContentWrapper').find('.timestampContent').html()
                }
            });
            return posts;
        });
    }
    GetFbPosts('https://www.facebook.com/pg/officialstackoverflow/posts/').then(posts=>{
        // Log all posts
        for (const post of posts) {
            console.log(post.created_at, post.message);
        }
    });
    

    For more information and an example of how to retrieve more than 20 posts see: https://stackoverflow.com/a/54267937/2879085

提交回复
热议问题