1

I have a facebook page, and I am trying to upload a video to it that is already hosted on my servers. I need to do this via javascript, and all I have is the src link (something like https://cdn.whodaman.net/Q45rt7y.mp4) of the video. Being the administrator, I have all required permissions (publish_stream, manage_pages).

The facebook api says that I have to send the data as multipart/form-data which means the video content. So to do this via ajax, I followed this question on stackoverflow, and followed How to send FormData objects with Ajax-requests in jQuery.

Here's my javascript code:

var fd = new FormData();
fd.append( 'source', 'https://cdn.whodaman.net/Q45rt7y.mp4' );
fd.append( 'access_token', testaccessToken);
fd.append( 'title', "Test Video");
$.ajax({
  url: "https://graph-video.facebook.com/"+testPageId+"/videos",
  data: fd,
  processData: false,
  contentType: false,
  type: 'POST',
  beforeSend: function(xhr) { 
    xhr.setRequestHeader('Content-Type', 'multipart/form-data'); 
  }
});

Here's the request and corresponding response:

    Request URL:https://graph-video.facebook.com/[my page id]/videos
    Request Method:POST
    Status Code:400 Bad Request
Request Headers Accept:*/* Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en;q=0.8 Cache-Control:no-cache Connection:keep-alive Content-Length:948 Content-Type:multipart/form-data Cookie: [some cookie data] Host:graph-video.facebook.com Origin:http://real.domain.com:8090 Pragma:no-cache Referer:http://real.domain.com:8090/test/upload User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36 Request Payload ------WebKitFormBoundaryd8laVBo5HXRTFJnn Content-Disposition: form-data; name="source" https://cdn.whodaman.net/Q45rt7y.mp4 ------WebKitFormBoundaryd8laVBo5HXRTFJnn Content-Disposition: form-data; name="access_token" [my access token] ------WebKitFormBoundaryd8laVBo5HXRTFJnn Content-Disposition: form-data; name="title" Test Video ------WebKitFormBoundaryd8laVBo5HXRTFJnn-- Response Headers Access-Control-Allow-Origin:* Cache-Control:no-store Connection:keep-alive Content-Length:146 Content-Type:application/json; charset=UTF-8 Date:Mon, 11 Nov 2013 13:50:45 GMT Expires:Sat, 01 Jan 2000 00:00:00 GMT Pragma:no-cache WWW-Authenticate:OAuth "Facebook Platform" "invalid_token" "An access token is required to request this resource." X-FB-Debug:HlhHF7eIBkLbUBktqeWnVv8V3viIeS8jom0WPt1D7fc= X-FB-Rev:1000997

Facebook is asking for the access token! I'm pretty sure I have the right one, because I'm being able to add a text status post. I have also tried to change the name of the file parameter in form data from source to file to no effect.

So is it even possible to upload a video to facebook via the js sdk directly from the video url? I have a feeling it is, and am quite close to the solution.

Community
  • 1
  • 1
Arturo
  • 1,039
  • 1
  • 10
  • 21
  • _“which means the video content”_ – but you are _not_ uploading the video’s content, but only its URL here. For photos it is possible to upload them using the parameter `url` instead of `source` – so I’d try if that works for videos too. (Has to be a “normal” POST then, not multipart.) – CBroe Nov 11 '13 at 14:22
  • But since facebook does not have a URL parameter for uploading video, this can't be done? I can't believe that, since uploading via an `input type='file'` technically does stream the data from a source. – Arturo Nov 11 '13 at 14:28
  • I said I would _try_ that, I did not say it’s guaranteed to work. (The `url` parameter for photo uploads is barely documented either.) And maybe they designed the video upload feature mostly with “fresh” videos people would upload from their own device in mind, and not stuff that’s floating around the web already … – CBroe Nov 11 '13 at 14:41
  • my videos are fresh! only that they have already been uploaded someplace else. i hope they come out with a url parameter soon. – Arturo Nov 11 '13 at 14:44
  • @Atharva Johri i have same issue. u r solved this? if u find some solution please help to me. – gokhan Jan 25 '15 at 12:58
  • @gokhan i used server side integration to accomplish this. If you are still interested, let me know and i'll point you to my github repo. – Arturo Jan 27 '15 at 13:29
  • @Atharva Johri yes i'm interesting. be pleased thanks. – gokhan Jan 28 '15 at 23:14
  • @AtharvaJohri i'm still waiting your return. i found your github account when u upload please write. Thanks bro. – gokhan Feb 01 '15 at 11:51
  • @AtharvaJohri when u will be free dude. i still wait u. :) – gokhan Feb 09 '15 at 12:33
  • @gokhan hey - check out the `uploadMediaToFacebook` method here https://github.com/atharvajohri/minerva/blob/master/Minerva/grails-app/controllers/com/minerva/MediaController.groovy I am making use of restfb, and expecting the access token in the request – Arturo Feb 11 '15 at 06:53
  • @AtharvaJohri u have skype? – gokhan Feb 11 '15 at 13:35
  • Hi @gokhan - i have shown you the code and told you the steps.. try solving the problem yourself now - it is not that difficult :) – Arturo Feb 16 '15 at 07:35

1 Answers1

3

Perform a POST request to the graph api Using the file_url field to specify the url for your video and get rid of the source field , no need for a multipart/form-data .

Radwan Diab
  • 124
  • 8