Upload Base64 Image Facebook Graph API

前端 未结 7 2260
礼貌的吻别
礼貌的吻别 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:22

    Here is how I was able to post an image to facebook using the facebook JS API. I am using the canvas HTML5 functionality. It's not fully supported by every browser.

    You need first to get the image data. Then to encapsulate it in a form data. I then use the FB.login API in order to retrieve the access token and the userID.

              var data = $('#map >> canvas').toDataURL('image/png');
              var blob;
              try {
                var byteString = atob(data.split(',')[1]);
                var ab = new ArrayBuffer(byteString.length);
                var ia = new Uint8Array(ab);
                for (var i = 0; i < byteString.length; i++) {
                  ia[i] = byteString.charCodeAt(i);
                }
                blob = new Blob([ab], {type: 'image/png'});
              } catch (e) {
                console.log(e);
              }
              var fd = new FormData();
              fd.append("source", blob);
              fd.append("message", "Photo Text");
              FB.login(function(){
                var auth = FB.getAuthResponse();
                $.ajax({
                  url:"https://graph.facebook.com/"+auth.userID+"/photos?access_token=" + auth.accessToken,
                  type:"POST",
                  data:fd,
                  processData:false,
                  contentType:false,
                  cache:false,
                  success:function(data){
                    console.log("success " + data);
                  },
                  error:function(shr,status,data){
                    console.log("error " + data + " Status " + shr.status);
                  },
                  complete:function(){
                    console.log("Ajax Complete");
                  }
                });
              }, {scope: 'publish_actions'});
    

提交回复
热议问题