PHPism Blog

Converting everything into PHP code.

Fix Facebook Video Thumbnail issue

Dear all;
After the new upgrades in Facebook Open Graph some old features got limited. Collecting Facebook video thumbnail now require some additional steps to be done.
We are now publishing a fast fix until we collect all problems in the current version and post all these patches in addition to the new features in the next version.
A- You need to get an access token:
1. Get User Short-Lived Access Token: Go to the Graph API Explorer: https://developers.facebook.com/tools/explorer
Select the application you want to get the access token for (in the “Application” drop-down menu, not the “My Apps” menu).
Click “Get Token” > “Get User Access Token”.
In the pop-up, under the “Extended Permissions” tab, check under “Events, Groups & Pages” a) ads_management b) manage_pages.
Click “Get Access Token”.
Grant access from a Facebook account that has access to manage the target page. Note that if this user loses access the final, never-expiring access token will likely stop working.
Token that appears in the “Access Token” field is your short-lived access token.

2. Generate Long-Lived Access Token
Following these instructions : https://developers.facebook.com/docs/facebook-login/access-tokens#extending from the Facebook docs, make a GET request to

https://graph.facebook.com/v2.8/oauth/access_token?grant_type=fb_exchange_token&client_id={app_id}&client_secret={app_secret}&fb_exchange_token={short_lived_token}

entering in your app’s ID and secret and the short-lived token generated in the previous step.
You cannot use the Graph API Explorer. For some reason it gets stuck on this request. I think it’s because the response isn’t JSON, but a query string. Since it’s a GET request, you can just go to the URL in your browser.
The response should look like this:

access_token=ABC123&expires=5182959

“ABC123” will be your long-lived access token. You can put it into the Access Token Debugger : https://developers.facebook.com/tools/debug/accesstoken to verify. Under “Expires” it should have something like “2 months”.

B- Open submit.php and /m/submit.php in case you have mobile module installed.
Find:
$media_url = "https://graph.facebook.com/".$video_id."/picture";

Replace with:
$your_access_token = "";
$graph_url = "https://graph.facebook.com/v2.9/".$video_id."/thumbnails?access_token=".$your_access_token;
$thumbnails_data = json_decode(file_get_contents($graph_url));
if(isset($thumbnails_data->data))
{
$thumbnails = $thumbnails_data->data;
foreach($thumbnails as $thumbnail)
{
if($thumbnail->is_preferred == true)
{
$media_url = $thumbnail->uri;
}
}
if(!isset($media_url))
{
$media_url = $thumbnails[0]->uri;
}
}

Best Regards

Comments are currently closed.