4

i'm trying to upload video on Facebook page using this code. I have added all the compulsory credentials to add video. but when i select video and click upload button it json request says you dont have permission to upload video. I tried to debug this problem but couldn't succeed. please help me out. thank you

 <?php
$app_id = "xxxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_url = "http://xxxxxxxx/test.php";
$video_title = "test_video_for_app";
$video_desc = "test_description";
$page_id = "xxxxxxxxxxxxxxxx"; // Set this to your APP_ID for Applications

$code = $_REQUEST["code"];

echo '<html><body>';

if(empty($code)) {
  // Get permission from the user to publish to their page. 
  $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=". $app_id . "&redirect_uri=" . urlencode($my_url). "&scope=publish_actions,manage_pages";
  echo('<script>top.location.href="' . $dialog_url . '";</script>');
} else {

  // Get access token for the user, so we can GET /me/accounts
  $token_url = "https://graph.facebook.com/oauth/access_token?client_id=". $app_id . "&redirect_uri=" . urlencode($my_url). "&client_secret=" . $app_secret. "&code=" . $code;
  $access_token = file_get_contents($token_url);

  $accounts_url = "https://graph.facebook.com/me/accounts?" . $access_token;
  $response = file_get_contents($accounts_url);

  // Parse the return value and get the array of accounts we have
  // access to. This is returned in the data[] array. 
  $resp_obj = json_decode($response,true);
  $accounts = $resp_obj['data'];

  // Find the access token for the page to which we want to post the video.
  foreach($accounts as $account) {
       if($account['id'] == $page_id) {
       $access_token = $account['access_token'];
       break;
    }
  }

  // Using the page access token from above, create the POST action
  // that our form will use to upload the video.
  $post_url = "https://graph-video.facebook.com/" . $page_id . "/videos?". "title=" . $video_title. "&description=" . $video_desc. "&access_token=". $access_token;

  // Create a simple form 
  echo '<form enctype="multipart/form-data" action=" '.$post_url.' "  
   method="POST">';
  echo 'Please choose a file:';
  echo '<input name="file" type="file">';
  echo '<input type="submit" value="Upload" />';
  echo '</form>';

}

echo '</body></html>';

?>

this is the responce coming from facebook

    {
       "error": {
      "message": "(#100) No permission to publish the video",
      "type": "OAuthException",
      "code": 100,
      "fbtrace_id": "H1uOf8K83lL"
   }
}
Striker
  • 948
  • 1
  • 13
  • 28

1 Answers1

6

so finally after a long research i got the solution. we just need to add permission from developers.facebook.com.

  1. fisrt go to developers.facebook.com
  2. go to tools and support and select graph api explorer
  3. then select your application and click on get access token
  4. add the permission publish_action in that and click ok
  5. u will get access token.

copy thataccess token and paste in place of

$access_token = file_get_contents($token_url);

it will be like

$access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

then run your code and its done

Striker
  • 948
  • 1
  • 13
  • 28
  • I did this and it works great for a few users. For some reason I have some users still getting this error? Any ideas why it would only affect some and not others? – John Pollard Nov 01 '16 at 01:03
  • @JohnPollard yes, the access token you are getting is temporery token valid for few hours. you need to create long term access token. for that you can got to http://stackoverflow.com/questions/10467272/get-long-live-access-token-from-facebook. – Striker Nov 03 '16 at 09:38
  • This access token is short-lived, you need to refresh that token immediately. – Satheesh Sep 26 '17 at 17:36
  • there is a method to get long-lived access token – Striker Sep 29 '17 at 11:57