如何使用facebookapi的publish_action权限

How to use publish_action permission of facebook api?

本文关键字:action 权限 publish 何使用 facebookapi      更新时间:2024-05-02

我想在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帐户时发布图片。但当我试图发布来自其他帐户的图像时,我得到了以下错误

"错误":{"message":"(#200)需要扩展权限:publish_actions","type":"OAuthException","代码":200,"fbtrace_id":"EAcb5VG/eFS"}

我认为这是facbookapi的许可问题。你能帮我管理facebook的"发布操作"权限吗?

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

";publish_actions"提供了代表使用您的应用程序的人发布帖子、Open Graph操作、成就、分数和其他活动的访问权限。

您的应用程序无需请求publish_actions权限即可使用Feed对话框、request对话框或Send对话框。

如果使用";publish_actions"你必须以另一种方式请求Facebook审查你的应用程序如何使用";publish_actions"准许