Javascript: Upload image from Canvas to FB via Graph API

谁说胖子不能爱 提交于 2020-01-24 08:28:27

问题


I am composing an image in a canvas, I get the base64 image data by using canvas.toDataURL('png') and trimming the additional information.

 var dataUrl = canvas.toDataURL('png');
 var escapedBase64Data = dataUrl.replace("data:image/png;base64,","");

After that I try to post to facebook using:

FB.api('/me/photos', 'post', { source:data});

Photos (https://developers.facebook.com/docs/reference/api/user/) has a source property. This is where you will place the data content (multipart/form-data) of your photo.

I convert my base64 encoded data to multipart/form-data by specifying the headers.

The result looks like this:

--0.2242348059080541
Content-Disposition: file; name="file"; filename="image.png"
Content-Type: image/png
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAAfQAAAH0CAYAAADL1t+KAAAbBElEQVR4Xu3dP4jre0LG4V2xsFVYEKy
...    
QAAAABJRU5ErkJggg==

--0.2242348059080541--

After I complete the FB api call I receive the following error:

Object {message: "(#324) Requires upload file", type: "OAuthException", code: 324} 

Any suggestions?

Thanks


回答1:


Here a working code example :

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 += f.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() {
    // error managment
};
xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundary);

//Send the request
xhr.sendAsBinary(formData);



回答2:


Here is an easy solution:

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'
    });
}
const upload = async (response) => {
    let canvas = document.getElementById('canvas');
    let dataURL = canvas.toDataURL('image/jpeg', 1.0);
    let blob = dataURItoBlob(dataURL);
    let formData = new FormData();
    formData.append('access_token', response.authResponse.accessToken);
    formData.append('source', blob);

    let responseFB = await fetch(`https://graph.facebook.com/me/photos`, {
        body: formData,
        method: 'post'
    });
    responseFB = await responseFB.json();
    console.log(responseFB);
};
document.getElementById('upload').addEventListener('click', () => {
    FB.login((response) => {
        //TODO check if user is logged in and authorized publish_actions
        upload(response);
    }, {scope: 'publish_actions'})
})

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



来源:https://stackoverflow.com/questions/15739555/javascript-upload-image-from-canvas-to-fb-via-graph-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!