如何使用facebook api的publish_action权限?

时间:2016-04-12 08:07:02

标签: javascript angularjs facebook facebook-graph-api

我想在facebook中发布base64编码图像。以下是我的代码

 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/me/photos?access_token=' + authToken, true);
        xhr.onload = xhr.onerror = function () {
          console.log(xhr.responseText);
        };
        xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
        if(!xhr.sendAsBinary){
          xhr.sendAsBinary = function(datastr) {
            function byteValue(x) {
              return x.charCodeAt(0) & 0xff;
            }
            var ords = Array.prototype.map.call(datastr, byteValue);
            var ui8a = new Uint8Array(ords);
            this.send(ui8a.buffer);
          }
        }
        xhr.sendAsBinary(formData);
        xhr.onreadystatechange = function () {
          if (xhr.readyState == 4 && xhr.status == 200) {
            alert('Your image has been successfully shared');
          }
        }
      };

      var data = yourDesigner.getProductDataURL();
      var encodedPng = data.substring(data.indexOf(',') + 1, data.length);
      var decodedPng = Base64Binary.decode(encodedPng);
      // var decodedPng = dataURItoBlob(test,"png");
      FB.getLoginStatus(function (response) {
        var request = {};
        if (response.status === "connected") {
          request = postImageToFacebook(response.authResponse.accessToken, "test", "image/png", decodedPng, "www.captivations.com.au");
        } else if (response.status === "not_authorized") {
          FB.login(function (response) {
            request = postImageToFacebook(response.authResponse.accessToken, "test", "image/png", decodedPng, "www.captivations.com.au");
          }, {scope: "publish_actions"});
        } else {
          FB.login(function (response) {
            request = postImageToFacebook(response.authResponse.accessToken, "test", "image/png", decodedPng, "www.captivations.com.au");
          }, {scope: "publish_actions"});
        }
      });

我可以在登录Facebook帐户时发布图片。但是,当我试图发布其他帐户的图像时,我得到了以下错误

  

&#34;错误&#34;:{         &#34; message&#34;:&#34;(#200)需要扩展权限:publish_actions&#34;,         &#34;输入&#34;:&#34; OAuthException&#34;,         &#34;代码&#34;:200,         &#34; fbtrace_id&#34;:&#34; EAcb5VG / eFS&#34;      }

我认为它是facbook api的许可问题。你能帮我管理&#34;发布行动&#34; facebook的许可?

1 个答案:

答案 0 :(得分:0)

来自https://developers.facebook.com/docs/facebook-login/permissions

  

&#34; publish_actions&#34;提供使用您的应用代表个人发布帖子,开放图表操作,成就,分数和其他活动的权限。

     

您的应用无需请求publish_actions权限即可使用Feed对话框,请求对话框或发送对话框。

如果您使用&#34; publish_actions&#34;以另一种方式获得许可,您必须要求Facebook审核您的应用如何使用&#34; publish_actions&#34;权限。