Reputation: 1089
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.
Upvotes: 1
Views: 5495
Reputation: 134
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 .
Upvotes: 4