Uploading video to facebook via JS SDK - facebook-graph-api

To make a long story short - I'm trying to upload an mp4 to facebook via the Javascript SDK. According to the documentation I should be able to do this:
/* make the API call */
FB.api(
"/me/videos",
"POST",
{
"source": "{video-data}"
},
function (response) {
if (response && !response.error) {
/* handle the result */
}
}
);
However - no matter what I pass through as a source I keep getting back an error object that says "(#353) You must select a video file to upload." Is there something missing from this method that is causing this?

Related

Retrieving the progress of getObject (aws-sdk)

I'm using node.js with the aws-sdk (for S3).... When I am downloading a huge file from s3, how can I regularly retrieve the progress of the download so that the front-end can show a progress bar? Currently I am using getObject. (https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getObject-property)
The code to download the file works. Here's a snippet of my code...
return await new Promise((resolve, reject) => {
this.s3.getObject(params, (error, data) => {
if (error) {
reject(error);
} else {
resolve(data.Body);
}
});
I'm just not sure how to hook into the progress as it's downloading. Thanks in advance for any insight!
You can utilize S3 byte-range fetching which allows the fetching of small parts of a file in S3. This capability then allows us to fetch large objects by dividing the file download into multiple parts which brings the following advantages:
Part download failure does not require full re-downloading of the file.
Download pause/resume capability.
Download progress tracking
Retry packets that failed or interrupted by network issues
Sniff headers located in the first few bytes of the file if we just need to get metadata from the files.
You can split the file download by your size of choice (I propose 1-4mb at a time) and download the parts chunk by chunk, when each of the get object promises complete, you can trace how many have completed. A good start is by looking at the AWS documentation.
STREAMING OPTION
Another option is to use a stream and track the amount of bytes received:
const { ContentLength: contentLength } = await s3.headObject(params).promise();
const rs = s3.getObject(s3Params).createReadStream();
let progress = 0;
rs.on('data', function (chunk) {
// Advance your progress by chunk.length
progress += chunk.length;
console.log(`Progress: ${progress / contentLength}%`);
});
// ... pipe to write stream

Thumbnail the first page of a pdf from a stream in GraphicsMagick

I know how to use GraphicsMagick to make a thumbnail of the first page of a pdf if I have a pdf file and am running gm locally. I can just do this:
gm(pdfFileName + "[0]")
.background("white")
.flatten()
.resize(200, 200)
.write("output.jpg", (err, res) => {
if (err) console.log(err);
});
If I have a file called doc.pdf then passing doc.pdf[0] to gm works beautifully.
But my problem is I am generating thumbnails on an AWS Lambda function, and the Lambda takes as input data streamed from a source S3 bucket. The relevant slice of my lambda looks like this:
// Download the image from S3, transform, and upload to a different S3 bucket.
async.waterfall([
function download(next) {
s3.getObject({
Bucket: sourceBucket,
Key: sourceKey
},
next);
},
function transform(response, next) {
gm(response.Body).size(function(err, size) { // <--- gm USED HERE
.
.
.
Everything works, but for multipage pdfs, gm is generating a thumbnail from the last page of the pdf. How do I get the [0] in there? I did not see a page selector in the gm documentation as all their examples used filenames, not streams I believe there should be an API, but I have not found one.
(Note: the [0] is really important not only because the last page of multipage PDFs are sometimes blank, but I noticed when running gm on the command line with large pdfs, the [0] returns very quickly while without the [0] the whole pdf is scanned. On AWS Lambda, it's important to finish quickly to save on resources and avoid timeouts!)
You can use .selectFrame() method, which is equivalent to specifying [0] directly in file name.
In your code:
function transform(response, next) {
gm(response.Body)
.selectFrame(0) // <--- select the first page
.size(function(err, size) {
.
.
.
Don't get confused about the name of function. It work not only with frames for GIFs, but also works just fine with pages for PDFs.
Checkout this function source on GitHub.
Credits to #BenFortune for his answer to similar question about GIFs first frame. I've took it as inspiration and tested this solution with PDFs, it actually works.
Hope it helps.

Facebook Javascript API unknown path components

I'm trying to integrate Facebook sharing into a webpage using this code edited from their official developer tutorial, but I'm not having any luck.
function postToFacebook(item_id)
{
FB.api(
'/me/completeset:display',
'post',
{ item: 'http://completeset.us/item/'+item_id },
function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Share was successful! Action ID: ' + response.id);
}
}); //End api
}
I've got the app set up, with an "item" object and the "defined" action defined. I haven't been able to submit the action yet though, because I haven't been able to post anything with it yet. I've verified that the item_id is being passed in correctly, and I've debugged the url using their debugging tool. When I call this function I'm getting unknown path components/ completeset:display. I haven't been able to find much information on this error, so I'm wondering: what are the causes, and how might I be able to fix it?
The names mismatched. The getCode link for the action on facebook displayed the name of the action as being show_off for some reason. It was the same problem as the linked question. I experimented with the app backend and found that if you create an action with one name and then change the name of it, it will display with the new name but you must still reference it using the old name in the code.

View attachments in threads

I'm currently working on an alternative way to view the threads and messages. But I have problems figuring out how to display the images attached to a message.
I have a GET request to this url: https://graph.facebook.com/t_id.T_ID/messages?access_token=ACCESS_TOKEN. And the response includes
"attachments": {
"data": [
{
"id": "df732cf372bf07f29030b5d44313038c",
"mime_type": "image/jpeg",
"name": "image.jpg",
"size": 76321
}
]
}
but I can't find any way to access the image.
Thanks
Support for this hasn't yet been added to the Graph API and as with many of the other messaging APIs, it's currently only avaialable for testing (i.e you must be a developer of the app to use it presently)
There's an undocumented REST API endpoint for this, which should work for any app (that you're the developer of, as above).
To use the REST method to get the attachment data, it's
https://api.facebook.com/method/messaging.getattachment
With parameters:
access_token=YOUR_ACCESS_TOKEN
mid=MESSAGE_ID
aid=ATTACHMENT_ID
format=json //(it defaults to XML otherwise)
The response is like this:
{"content_type":"image\/png","filename":"Screen Shot 2012-02-08 at 11.35.35.png","file_size":42257,"data":<FILE CONTENTS>}
I've just tested this and it worked OK for me, taking the <FILE CONTENTS> and base64 decoding them gave me back the original image correctly

Open Graph Tutorial - Get New Action Code Snippet

I'm sorry if the question is a newbie question but I can't seem to find the answer.
I'm following the Open Graph Tutorial. I am in Step 4: Publish Actions. I am at the chapter Publish an Action. The tutorial says:
"The Open Graph Dashboard page has a 'Get Code' link next to your action. This contains curl code snippets that you can copy into terminal and run directly."
Here is my curl code snippet:
curl -F 'access_token=(myaccess_token_replaced)' \
-F 'podcast=http://samples.ogp.me/(code replaced)' \
'https://graph.facebook.com/me/myapname:listen'
What do I do with this code snippet? I entered the code snippet in my PuTTY terminal and all I got back was a response:
{"id":"341348384144"} (not the actual id number)
I thought it would return more code than that. The tutorial has a new block of code in the example.
<script type="text/javascript">
function postCook()
{
FB.api('/me/YOUR_NAMESPACE:cook' +
'?recipe=http://example.com/cookie.html','post',
function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post was successful! Action ID: ' + response.id);
}
});
}
</script>
Do I hand code this and just replace the response.id with the id code that I got back from the curl code snippet?
Please / Thanks.
I believe that ID is the identifier for the action you just posted. It should be unique each time you HTTP post to that url. I believe the CURL code was to be for you to play with just like the Graph API explorer tool is used to play with the graph API. The javascript code you posted above is another example of how to accomplish the same thing that the CURL code did (at least it appear that way to me)