Obtaining a Facebook auth token for a command-line (desktop) application

后端 未结 4 1599
梦谈多话
梦谈多话 2020-12-17 09:38

I am working for a charity which is promoting sign language, and they want to post a video to their FB page every day. There\'s a large (and growing) number of videos, so th

4条回答
  •  执念已碎
    2020-12-17 10:07

    From the facebook video API reference:

    An individual Video in the Graph API.

    To read a Video, issue an HTTP GET request to /VIDEO_ID with the user_videos permission. This will return videos that the user has uploaded or has been tagged in.

    Video POST requests should use graph-video.facebook.com.

    So you should be posting to graph-video.facebook.com if you are to upload video.

    You also need extended permissions from the user or profile you'll be uploading to, in this case you need video_upload this is going to be requested once only, when the user currently logged in is asked for such permission for the app.

    And your endpoint should be:

    https://graph-video.facebook.com/me/videos

    If you always want to post to a specific user than you'll have to change the endpoint part from /me to the User ID or page ID.

    Here's a sample (in PHP):

    $app_id = "YOUR_APP_ID";
    $app_secret = "YOUR_APP_SECRET"; 
    $my_url = "YOUR_POST_LOGIN_URL"; 
    $video_title = "YOUR_VIDEO_TITLE";
    $video_desc = "YOUR_VIDEO_DESCRIPTION";
    
    $code = $_REQUEST["code"];
    
    if(empty($code)) {
       $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
         . $app_id . "&redirect_uri=" . urlencode($my_url) 
         . "&scope=publish_stream";
        echo("");
    }
    
    $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);
    
    $post_url = "https://graph-video.facebook.com/me/videos?"
        . "title=" . $video_title. "&description=" . $video_desc 
        . "&". $access_token;
    
    echo '
    '; echo 'Please choose a file:'; echo ''; echo ''; echo '
    ';

    Although I'm concerned about the upload speed if the videos are too big, but I'm guessing your customer has already sorted that out (compress/optimize/short videos etc.)

    I've made you a demo here. Go to my website (I own that domain) and try to upload a video. I tried with this one which is a relatively small 4Mb file. Be sure that this script will only try to upload a video, nothing more (to the FB profile you are currently logged in, that is) but, if you are still concerned, copy my snippet, upload it to your own server (with PHP support of course) and create a test app where the site url is that domain and be sure to specify in the $my_url variable your endpoint which is basically the full path to your script receiving responses from facebook:

    http://yourdomain.com/testfb.php 
    

    If you still want to do it on a desktop app then you have to go to developer.facebook.com on your app settings:

    Settings > Advanced
    

    And look for the first option:

    enter image description here

    And enable that switch so that facebook allows you to POST from a desktop or native app instead of a web server.

    Note: I'm not an expert on Ruby, but the above working PHP code should be pretty obvious and easy to port to it.

提交回复
热议问题