douwei7203 2014-02-06 19:15
浏览 121

图形API发布base64 IMAGE到Facebook

Trying to post some data to my wall and am coming up with Error

I've been referencing this post mostly: Upload Base64 Image Facebook Graph API

But am stuck and keep receiving a 400 Error response from FB.
And in reference to that, I check my call and my data object is empty (which is what I'm assuming is causing the 400 error).

Here's what I have:

function publishStream(img)
        {
            var accessToken = "";

            FB.getLoginStatus(function(response) 
            {
              if (response.status === 'connected') 
              {
                var uid = response.authResponse.userID;
                accessToken = response.authResponse.accessToken;
              } 
              else if (response.status === 'not_authorized') {
                // the user is logged in to Facebook, 
                // but has not authenticated your app
              } 
              else {
                // the user isn't logged in to Facebook.
              }
            });

            var imageData  = img;
            try{
                blob = dataURItoBlob(imageData);
            }
            catch(e){
                console.log(e);
            }

            var fd = new FormData();
            fd.append("access_token",accessToken);
            fd.append("source", blob);
            fd.append("message","");

            try{
                $.ajax({
                    url:"https://graph.facebook.com/me/photos?access_token=" + 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("Posted to facebook");
                    }
                });

            }
            catch(e){
                console.log(e);
            }
        }

        function dataURItoBlob(dataURI) {
            var byteString = window.atob(dataURI);

            var ia = new Uint8Array(byteString.length);
            for (var i = 0; i < byteString.length; i++) {
                ia[i] = byteString.charCodeAt(i);
            }
            var blob = new Blob([ia], { type: 'image/png' });

            return blob;
        }

Where the parameter img is a base64 encoded image.

This is my resulting data from the Bad Request:

{ "data": [   ] }

Can anyone guide me to the finish line??? Any help is appreciated!

  • 写回答

1条回答 默认 最新

  • dtla92562 2014-02-07 09:14
    关注

    Have you read the docs on FB under https://developers.facebook.com/docs/graph-api/reference/user/photos/#publish

    Apparantly, the sourceblob needs to be encapsuled with escaped curly brackets:

    source=%7Bimage-data%7D
    

    What happens if you try

    fd.append("source", '%7B' + blob + '%7D');
    

    Also, on one of the comments to the answer at Upload Base64 Image Facebook Graph API it's stated to " remove the "data:image/png;base64," portion when you send it for blob conversion". Did you try this?

    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制