开发者

Get public page statuses using Facebook Graph API without Access Token

开发者 https://www.devze.com 2023-04-10 20:11 出处:网络
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

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.facebook.com/tools/explorer/?method=GET&path=microsoft%2Fstatuses - I need an access token. As the Microsoft page is 'public', is this definitely the case? Is there no way for me to access these public status' without an access token?

If this is the case, how is the correct method of creating an access token for my website? I have an App ID, however all of the examples at http://developers.facebook.com/docs/authentication/ describe handling user login. I simply want to get the latest status update on the Microsoft p开发者_开发百科age and display it on my site.


This is by design. Once it was possible to fetch the latest status from a public page without access token. That was changed in order to block unidentified anonymous access to the API. You can get an access token for the application (if you don't have a Facebook application set for your website - you should create it) with the following call using graph API:

https://graph.facebook.com/oauth/access_token?
client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&
grant_type=client_credentials  

This is called App Access Token. Then you proceed with the actual API call using the app access token from above.

hope this helps


You can use AppID and Secret key to get the public posts/feed of any page. This way you don't need to get the access-token. Call it like below.

https://graph.facebook.com/PAGE-ID/feed?access_token=APP-ID|APP-SECRET

And to get posts.

https://graph.facebook.com/PAGE-ID/posts?access_token=APP-ID|APP-SECRET


It's no more possible to use Facebook Graph API without access token for reading public page statuses, what is called Page Public Content Access in Facebook API permissions. Access token even is not enough. You have to use appsecret_proof along with the access token in order to validate that you are the legitimate user. https://developers.facebook.com/blog/post/v2/2018/12/10/verification-for-individual-developers/. If you are individual developer, you have access to three pages of the data (limited), unless you own a business app.


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


I had a similar use case for some weeks and I used this API:

https://rapidapi.com/axesso/api/axesso-facebook-data-service/

I could fetch all posts and comments in some minutes, worked quite well for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号