How to upload video on facebook using FB.ui Javasc

2019-03-30 01:27发布

问题:

I'm using FB.ui method to post image to facebook as described below:

FB.ui({
    display: 'popup',
    method: 'feed',
    name: 'image',
    link: 'link',
    picture: 'image path',
    caption: 'caption',
    description: 'description'
}, function(response){
   // do something
});

I'm able to post image successfully, but couldn't post video. I have tried, but failed.

FB.ui({
    display: 'popup',
    method: 'feed',
    link: 'link',
    picture: 'thumbnail image path',
    source: 'https://example.com/media/video.mp4',
    caption: 'caption',
    description: 'description'
}, function(response){
    // do something
});

above approach is posting feed, I'm looking video should play on facebook only instead of taking link of page.

I am not sure whether I'm missing something on video post OR approach it self wrong.

Could someone help on video, I would really appreciate.

Thanks

回答1:

You can upload videos to Facebook, using the Graph API, in multiple ways. You can have resumable and non-resumable uploads.

The latter is the easiest; you post to graph-video.facebook.com and the video data must be multipart/form-data encoded. The files are limited to 1GB in size and 20 minutes long.

You can upload videos using the SDKs. For example, the following code will use the JS SDK:

/* make the API call */
FB.api(
    "/{user-id}/videos",
    "POST",
    {
        "source": "{video-data}"
    },
    function (response) {
      if (response && !response.error) {
        /* handle the result */
      }
    }
);

Here the source parameter is your encoded video file. See the docs for more info. Alternatively, if the video is already uploaded somewhere, you can use the file_url parameter to provide a link to that video.

Please note: the JS SDK default to graph.facebook.com, but you need to post to graph-video.facebook.com. So either you need to override the domain, or re-create the post with a normal JS http request. In that case, use the source parameter and add your access_token in a parameter with that name.

If you have more control over your video files and the process, you can upload the files in chunks. That will allow you to recover from lost upload segments, without the need to re-upload the whole file.