Post image on facebook via javascript

384 views Asked by At

I am trying to post picture on facebook using the facebook JS sdk.

The image I want to post is an image from a canvas, I am using the following code:

function publishFacebook(){
  console.log('dentro publish');
  var dataURL = canvas.toDataURL()
  var onlyData = dataURL.substring(dataURL.indexOf(',')+1, dataURL.length);
  var decoded = Base64Binary.decode(onlyData);
  var imageIwillPost = getFormData2(decoded, "aaa", "bbb.png");
  console.log(imageIwillPost);
  FB.api('/me/photos', 'POST',
        {'source': '{' + imageIwillPost + '}' ,
         'message': 'Whatever message we decide'},
         function(resp) {
           console.log('into function');
           if (resp && !resp.error) {
             console.log('uploaded');
             console.log(resp);
           } else {
             console.log('some error');
             console.log(resp.error);}});}

function getFormData2(imageData, name, filename){
  var boundary = 'AaB03x';
  var formData = '';
  var formData = 'Content-Type: multipart/form-data; boundary=' + boundary + '\r\n';
  formData += '--' + boundary + '\r\n'
  formData += 'Content-Disposition: file; name="' + name + '"; filename="' + filename + '"\r\n';
  formData += 'Content-Type: ' + 'image/png' + '\r\n';
  formData += 'Content-Transfer-Encoding: binary'+ '\r\n';
  formData += '\r\n';
  for ( var i = 0; i < imageData.length; ++i ){
    formData += String.fromCharCode( imageData[ i ] & 0xff );}
  formData += '\r\n';
  formData += '--' + boundary + '--' + '\r\n';

  return formData;}

However it doesn't work and return something like: Object {error_code: 1, error_msg: "An unknown error occurred"} However sometimes it just return that I am not authorized...

0

There are 0 answers