Reputation: 1155
I'm trying to pull the latest status from a Facebook page with the PHP SDK then insert it as a post into my website via a cron job, but seem to be stuck on the access token part. Here's what I have so far:
require 'facebook-php-sdk-master/src/facebook.php';
require 'wp-load.php';
// I replaced these obviously
$facebook = new Facebook(array(
'appId' => FB_APP_ID,
'secret' => FB_SECRET,
));
try {
$feed = $facebook->api('/322522705880/feed?limit=1');
} catch (FacebookApiException $e) {
error_log($e);
}
Then I use $feed['data'][0]['description'] etc to create the post with. However, the only time I can get $feed to have any data is when I reinstall a Facebook Wordpress plugin that must activate an access token some how because it will then work for a few hours, and then stop. I can't find any info anywhere at all on how I would generate one of my own from a script, https://developers.facebook.com/docs/reference/php/facebook-getAccessToken/ doesn't really provide any helpful info. If anyone can point me in the right direction, that would be great. The wordpress plugin is doing this somehow, I just need to know how to replicate it.
I did some poking around in the plugin and found this
$token = false;
if (!$token) {
// Get temporary token
$token = $this->model->fb->getAccessToken();
if (!$token) return false;
// Exchange it for the actual long-term token
$url = "https://graph.facebook.com/oauth/access_token?client_id={$app_id}&client_secret={$app_secret}&grant_type=fb_exchange_token&fb_exchange_token={$token}";
$page = wp_remote_get($url, array(
'method' => 'GET',
'timeout' => '5',
'redirection' => '5',
'user-agent' => 'wdfb',
'blocking' => true,
'compress' => false,
'decompress' => true,
'sslverify' => false
));
if(is_wp_error($page)) return false; // Request fail
if ((int)$page['response']['code'] != 200) return false; // Request fail
parse_str($page['body'], $response);
$token = isset($response['access_token']) ? $response['access_token'] : false;
if (!$token) return false;
}
But that gives me "No user access token specified" and I'm not sure how to use or get a user access token for that?
Upvotes: 2
Views: 1907
Reputation: 47976
All you need to have for this is an application token. You can "build" an app access token using your app_id and app_secret:
access_token=APP_ID|APP_SECRET
That's a pipe character |
between the two values.
Once you have this, you can just make a request to an endpoint like this:
https://graph.facebook.com/cocacola/feed?limit=1&access_token=APP_ID|APP_SECRET
The response will be something like this:
{
"data": [
{
"id": "40796308305_10152854866613306",
"from": {
"name": "Verone Diedericks",
"id": "100003842548992"
},
"to": {
"data": [
{
"category": "Food/beverages",
"name": "Coca-Cola",
"id": "40796308305"
}
]
},
"message": "Vony nd kimo or just vony",
"privacy": {
"value": ""
},
"type": "status",
"created_time": "2013-11-26T20:13:11+0000",
"updated_time": "2013-11-26T20:13:11+0000"
}
],
"paging": {
"previous": "https://graph.facebook.com/40796308305/feed?limit=1&access_token=163586277060593|Rtsz7h6IiHu7Uva9D5S4VW0FKu8&since=1385496791&__previous=1",
"next": "https://graph.facebook.com/40796308305/feed?limit=1&access_token=163586277060593|Rtsz7h6IiHu7Uva9D5S4VW0FKu8&until=1385496790"
}
}
Upvotes: 3