65

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 page and display it on my site.

Kara
  • 6,115
  • 16
  • 50
  • 57
christofr
  • 2,680
  • 1
  • 18
  • 19

5 Answers5

64

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

Anatoly Lubarsky
  • 3,026
  • 1
  • 18
  • 16
  • 4
    After I get the application token and pass it to "https://graph.facebook.com/PADEID/statuses?access_token=" I get the error "A user access token is required to request this resource." – pdavis Dec 28 '11 at 16:16
  • the response i get from a call to this url is of type text/plain I'm confused – Peter Perháč Oct 28 '12 at 18:00
  • 11
    Yikes! Careful - don't publish your app secret anywhere public. It's a secret! This code is fine if it stays server-side though. – Tom Waddington Jan 10 '13 at 23:57
  • 10
    So is the resulting access token good forever? Do I have to do this for each client? Why require a token if you are just going to give them away free? A hoop-jumping exercise. :p – Julian Feb 05 '13 at 19:50
  • 2
    The secret token should not be used on the client side for security reason, see https://developers.facebook.com/docs/facebook-login/security/#appsecret – ocolot Oct 25 '14 at 19:34
  • Here is a great article about access tokens in Facebook API 2.0: http://www.devils-heaven.com/facebook-access-tokens/ – nalexn May 28 '15 at 14:34
  • I get the following error when issuing above "An unknown error has occurred" – Matthew Lock Jun 26 '15 at 07:06
  • Hi Anatoly. Thanks for this. I'd like to get post with image for https://www.facebook.com/artisanideasltd/, could you give me detail example? Thanks – sirius2013 Apr 03 '17 at 07:31
  • It's there a similar solution for page insights? – Adrian B Sep 05 '17 at 09:39
  • 2
    It looks like this will continue work for applications that have previously accessed this data, but new applications will be subject to approval. Additionally in my testing with a development app leads me to believe that without a user or page access token your application needs to be approved specifically. (Hard to submit an app review when a development app can't perform the action though) – Mark May 07 '18 at 00:14
  • app access token is not working to get public feed and posts.I think now user access token is must to get feed or post? Any one have updates how to get user access token using graph api? – Vaibhav Bhatia Jun 11 '18 at 05:33
  • **DOES NOT WORK** ```{ "error": { "message": "(#10) This endpoint requires the 'manage_pages' permission or the 'Page Public Content Access' feature. Refer to https://developers.facebook.com/docs/apps/review/login-permissions#manage-pages and https://developers.facebook.com/docs/apps/review/feature#reference-PAGES_ACCESS for details.", "type": "OAuthException", "code": 10, "fbtrace_id": "AOrqBQ9ZRwG8obAIIKuK72e" } }``` – Yuseferi Jan 19 '20 at 15:08
33

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
Richard de Wit
  • 7,102
  • 7
  • 44
  • 54
Hassan Siddique
  • 1,590
  • 14
  • 27
  • 1
    Is there an expiring time for this solution? – Vucko Apr 18 '16 at 12:21
  • 2
    No expiry time yet and it is working fine. But don't believe on Facebook API it keeps on changing their mind :) – Hassan Siddique Apr 18 '16 at 12:25
  • Thanks for the quick reply :) – Vucko Apr 18 '16 at 12:27
  • 4
    For anyone else that sees this answer, this type of access token is called an [App Access Token](https://developers.facebook.com/docs/facebook-login/access-tokens#apptokens) and they do state that combination of AppId and AppSecret for an access token in their documentation. – Turnerj Feb 11 '17 at 08:15
  • Thanks for this, but I can't get image of post, could you help how to get image of post? – sirius2013 Apr 03 '17 at 07:39
  • 7
    It seems facebook has now locked this down :( Getting the events of a page now requires a page access token – Tor Arne Apr 11 '18 at 22:22
  • 1
    Not just events either - it looks like getting feed data at all requires a page or user access token. – Mark May 07 '18 at 00:13
  • is it possible to get image with the text by this way ? – Md. Abutaleb May 23 '18 at 09:57
  • It does not work any more as Facebook announcement here [link](https://developers.facebook.com/docs/pages/) . we need an approval or use access tokens. – pouyada Jun 22 '18 at 17:52
  • 1
    The error message is: To use 'Page Public Content Access', your use of this endpoint must be reviewed and approved by Facebook. To submit this 'Page Public Content Access' feature for review please read our documentation on reviewable features: https://developers.facebook.com/docs/apps/review. – Samuel Prevost Jan 13 '19 at 10:31
  • This token work only for app related activities, For user activities and post you need to have user access token, for any page related activities you need to have page access token. – OyeHarish Jan 31 '19 at 12:08
  • does not work anymore : `{ "error": { "message": "(#10) This endpoint requires the 'manage_pages' permission or the 'Page Public Content Access' feature. Refer to https://developers.facebook.com/docs/apps/review/login-permissions#manage-pages and https://developers.facebook.com/docs/apps/review/feature#reference-PAGES_ACCESS for details.", "type": "OAuthException", "code": 10, "fbtrace_id": "AyMIAFAtq3JnkCn-Bn_2Ey1" } }` – Yuseferi Jan 19 '20 at 14:41
  • This endpoint requires the 'manage_pages' permission or the 'Page Public Content Access' feature. Refer to https://developers.facebook.com/docs/apps/review/login-permissions#manage-pages and https://developers.facebook.com/docs/apps/review/feature#reference-PAGES_ACCESS for details. – Tharindu Sathischandra Mar 09 '20 at 11:56
4

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.

tleo
  • 351
  • 3
  • 16
2

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

Forivin
  • 14,780
  • 27
  • 106
  • 199
0

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.

Vik
  • 324
  • 3
  • 9