Over the past few days, I have been completely baffled by this issue. If someone could point me in the right direction, I would appreciate it! I tried to figure out how to send image via facebook graph api.
I have an image downloaded from Facebook through their graphical API, which displays correctly in the canvas element. I modify this element by drawing text on it and then want to upload it back to facebook. I am stuck in downloading.
Here are the links I looked at that were useful, but I was still stuck:
Facebook Graph API - Uploading photos using JavaScript
Discusses the use of multipart / form-data to upload to facebook using an email request.
https://gist.github.com/andyburke/1498758
Code for creating multi-part form data and sending a request to facebook to post images.
This is the code that I use to try to send the image to facebook
if ( XMLHttpRequest.prototype.sendAsBinary === undefined ) {
XMLHttpRequest.prototype.sendAsBinary = function(string) {
var bytes = Array.prototype.map.call(string, function(c) {
return c.charCodeAt(0) & 0xff;
});
this.send(new Uint8Array(bytes));
};
}
function PostImageToFacebook( authToken, filename, mimeType, imageData, message )
{
console.log("filename " + filename);
console.log("mimeType " + mimeType);
console.log("imageData " + imageData);
console.log("message " + message);
var boundary = '----ThisIsTheBoundary1234567890';
var formData = '--' + boundary + '\r\n'
formData += 'Content-Disposition: form-data; name="source"; filename="' + filename + '"\r\n';
formData += 'Content-Type: ' + mimeType + '\r\n\r\n';
for ( var i = 0; i < imageData.length; ++i )
{
formData += String.fromCharCode( imageData[ i ] & 0xff );
}
formData += '\r\n';
formData += '--' + boundary + '\r\n';
formData += 'Content-Disposition: form-data; name="message"\r\n\r\n';
formData += message + '\r\n'
formData += '--' + boundary + '--\r\n';
var xhr = new XMLHttpRequest();
xhr.open( 'POST', 'https://graph.facebook.com/me/photos?access_token=' + authToken, true );
xhr.onload = xhr.onerror = function() {
console.log( xhr.responseText );
};
xhr.setRequestHeader( "Content-Type", "multipart/form-data; boundary=" + boundary );
xhr.sendAsBinary( formData );
console.log(formData);
}
Calling the PostImageToFacebook function has the following errors:
{
"error": {
"type": "Exception",
"message": "Your photos couldn't be uploaded. Photos should be less than 4 MB and saved as JPG, PNG, GIF or TIFF files.",
"code": 1366046
}
I am writing a Data form, the output of which I pasted below:
------ ThisIsTheBoundary1234567890
Content-Disposition: form data; name = "source"; file name = "MemeImage.png"
Content-Type: image / png
------ ThisIsTheBoundary1234567890
Content-Disposition: form data; name = "message"
Publishing a Memographic Image
------ ThisIsTheBoundary1234567890 -