Javascript Facebook API POST multipart / form-data

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).buffer); //depreciated warning
        this.send(new Uint8Array(bytes));
    };
}

// This function takes an array of bytes that are the actual contents of the image file.
// In other words, if you were to look at the contents of imageData as characters, they'd
// look like the contents of a PNG or GIF or what have you.  For instance, you might use
// pnglib.js to generate a PNG and then upload it to Facebook, all from the client.
//
// Arguments:
//   authToken - the user auth token, usually from something like authResponse.accessToken
//   filename - the filename you'd like the uploaded file to have
//   mimeType - the mime type of the file, eg: image/png
//   imageData - an array of bytes containing the image file contents
//   message - an optional message you'd like associated with the image

function PostImageToFacebook( authToken, filename, mimeType, imageData, message )
{
    console.log("filename " +  filename);
    console.log("mimeType " + mimeType);
    console.log("imageData " + imageData);
    console.log("message " + message);

    // this is the multipart/form-data boundary we'll use
    var boundary = '----ThisIsTheBoundary1234567890';

    // let encode our image file, which is contained in the var
    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 -

+4
2

for ( var i = 0; i < imageData.length; ++i )
{
    formData += String.fromCharCode( imageData[ i ] & 0xff );
}

formData += imageData;
0

, Canvas :

const dataURItoBlob = (dataURI) => {
    let byteString = atob(dataURI.split(',')[1]);
    let ab = new ArrayBuffer(byteString.length);
    let ia = new Uint8Array(ab);
    for (let i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
    }
    return new Blob([ia], {
        type: 'image/jpeg'
    });
}

, FormData:

formData.append('source', blob);

: http://www.devils-heaven.com/facebook-javascript-sdk-photo-upload-from-canvas/

0

Source: https://habr.com/ru/post/1534584/


All Articles