Upload Base64 Image Facebook Graph API

前端 未结 7 2222
礼貌的吻别
礼貌的吻别 2020-11-28 06:06

I\'m trying to upload a base64 image to a FaceBook page using Node.js. I have managed to get the upload working with all the multipart data etc should I read the file from t

7条回答
  •  渐次进展
    2020-11-28 06:34

    I did something very similar to your question. I had a webcam-snapshot that needed to be POSTed to a Facebook Fan Page. The setup was in a restaurant where people could take a picture and it would be posted onto the Restaurants Page. People would then see a QR code to the posted facebook-photo which they could choose to share on their own profile. Hope this can help somebody because I searched a lot to get to this working SOLUTION

    Note: My image is BASE64 encoded already.

    //imageData is a base64 encoded JPG
    function postSocial(imageData, message){       
            var ia = toUInt8Array(imageData);
            postImageToFacebook(mAccessTokenPage, "imageName", "image/jpeg",ia, message);
    }
    
    function toUInt8Array(dataURI) {
            // convert base64 to raw binary data held in a string
            // doesn't handle URLEncoded DataURIs
            var byteString = window.atob(dataURI);
    
            // write the bytes of the string to an ArrayBuffer
            //var ab = new ArrayBuffer(byteString.length);
            var ia = new Uint8Array(byteString.length);
            for (var i = 0; i < byteString.length; i++) {
                ia[i] = byteString.charCodeAt(i);
            }
            return ia;
        }
    
    function postImageToFacebook( authToken, filename, mimeType, imageData, message ) {        
            // this is the multipart/form-data boundary we'll use
            var boundary = '----ThisIsTheBoundary1234567890';
    
            // let's 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/ + {PAGE_ID} + "/photos?access_token=" + authToken, true );
            xhr.onload = function() {
                // ... Fill in your own
                //Image was posted 
               console.log(xhr.responseText);
            };
            xhr.onerror = function(){
                console.log("Error while sending the image to Facebook");
            };
            xhr.setRequestHeader( "Content-Type", "multipart/form-data; boundary=" + boundary );
            xhr.sendAsBinary( formData );
        }
    

提交回复
热议问题