I've never used Facebook's Graph API, or OAuth. I'm simply trying to get a public Facebook page's feed using the Graph API, but it requires an access token. I don't want to hassle the users to login and allow access to get their token. A Facebook app access token could be used to get a public feed, but I'm trying to do this entirely in Javascript, so I can't use the app secret to do so. I read somewhere that a Facebook app access token doesn't ever expire or change unless I manually reset the secret. Is this true? Would it be safe to just hard code in the Access Token? If not, is there some way I could authenticate an app to get the token without having to involve a user? Is there some type of generic app token I could use?
In the newest version of the Graph API (v2. 9), we're announcing features that simplify development, making it even easier to build apps and experiences with Facebook. We're providing free access to over 140 million places around the world, the same data that powers Facebook, Instagram, and Messenger.
You can access Page Insights by typing /insights after the name of your page in the URL field. This command will retrieve all of the Insights associated with your Page. Type "/insights" after your company name. Click the Submit button.
With the Facebook API, you can access and read public data for Facebook Pages that you are not the admin of. This data, including business metadata, public comments, and posts, can be used for competitive analysis and benchmarking.
If you're anything like me your clients won't want a standard Facebook likebox plugin, they'll want it all styled and customised their own way.
You don't need to spend all day going round the official documentation wondering if any of it applies to you for something simple like this, it's quite easy. The confusion arises because you'd assume that with all these keys and secret ids you'd have to get permission or authentication from the Facebook page you wanted to pull the feed from - you don't. All you need is a valid app and you can get the feed for any public page.
Set your app up in Facebook and it will give you an app id and an API key. Get the profile id for the public page feed you want, and that's all you need. You can then use the following code to retrieve the auth token and then then use that to return the feed data as a JSON object.
<?php function fetchUrl($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 20); // You may need to add the line below // curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); $feedData = curl_exec($ch); curl_close($ch); return $feedData; } $profile_id = "the_profile_id_of_the_page_you_want"; //App Info, needed for Auth $app_id = "your_app_id_in_here"; $app_secret = "your_app_secret_in_here"; //Retrieve auth token $authToken = fetchUrl("https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id={$app_id}&client_secret={$app_secret}"); $json_object = fetchUrl("https://graph.facebook.com/{$profile_id}/feed?{$authToken}");
Thanks to an edit someone suggested I reckon this code came from here (looks familiar anyway :) ) and there's some more info in the comments there that might help.
You can then parse the object, here's some code to do it in PHP based on this thread;
Handling data in a PHP JSON Object
$feedarray = json_decode($json_object); foreach ( $feedarray->data as $feed_data ) { echo "<h2>{$feed_data->name}</h2><br />"; echo "{$feed_data->message}<br /><br />"; }
To find out what's available to you in the json object you can output the url in a browser and copy/paste it into this useful json visualisation tool;
http://chris.photobooks.com/json/default.htm
Facebook app access token doesn't ever expire or change unless I manually reset the secret
That's correct.
App tokens do not expire unless the App Secret is reset. App access tokens are unique to each app.
Since public feeds take any valid access token, application tokens can be used.
Go to https://developers.facebook.com/tools/access_token and you would not require a flow. Then you can just hard code it in. The moment you reset the secret this method is void.
$access_token = '1111111111|2Cha_1-n5' $graph_url = "https://graph.facebook.com/Boo/posts?access_token=" . $access_token; $page_posts = json_decode(file_get_contents($graph_url), true);
Then iterate through the pages
foreach($page_posts['data'] as $post){ $post_link = $post['actions'][0]['link']; $page_id = $post['from']['id']; $page_name = $post['from']['name']; $message = ($post['message']) ? $post['message'] : " "; $name = ($post['name']) ? $post['name'] : " "; $story = ($post['story']) ? $post['story'] : " "; $post_time = $post['updated_time']; }
Source: http://philippeharewood.com/facebook/how-to-get-facebook-access-tokens-in-php-for-public-posts-the-basic-sauce/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With