Я хочу опубликовать / опубликовать изображение в кодировке base64 в Facebook с помощью Graph API.
Я попробовал приведенный ниже код, полученный из этой статьи здесь , но не работает!
эта ошибка появляется на консоли ..
jquery.min.js:4 POST https://graph.facebook.com/me/photos?access_token=........ 403
error Status 403
Posted to facebook
Мой полный HTML код:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<div id="imageBox"></div>
<script>
function PostImageToFacebook(authToken)
{
/* my custom code */
var imageData = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAIAAAAiOjnJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAMnSURBVHhe7doxa5VXHMDh++Y7CEXI5HIh3VxF6FAcMjt0U/AjCJ06OQmSxVWIS+mQJUsHJ5cMXcRJyCpKKDj4DfIa8VzaJL5SSn6Tz8OF+3/P/OO85x7u9PH94Qqu2tb4hislLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwS08f3h2P8zh39cfrw1ZjPrHe3nv805v/u973TpydjPrN7f+u3nTF/Z+xYJIRFQlgknLE2ruSMxYYdi4SwSHgVbiy9Ct+9mZ+/mP/cXCKsr08/35lu76y2x8I5rhs27Fjf8GF+tHd6d/+fqs4cn8xP90/v7s1HH8YKXyOsJX/P9x6fS+qck/nh4/loPHCZsBYcv5qPx7hkfvZyTFwirG9a35wOft3668nnz8H9aT2Wh+PX87sxcoGwln0+v/8ybV8bj9s706PdMQ8nq7dj4gJhLZoeXLog3f7x4qbFAmEtubm6NaZ/uba6MaYv5rd+G36dsBasf5jGxP8hLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwS/vNOwo5FQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVgtfoEc2NhOeMjF60AAAAASUVORK5CYII=';
document.getElementById('imageBox').innerHTML='<img src="'+imageData+'"/>';
///////////////////////////////////////////////////////
/* original function code */
try {
blob = dataURItoBlob(imageData);
}
catch(e) {
console.log(e);
}
var fd = new FormData();
fd.append("access_token",authToken);
fd.append("source", blob);
fd.append("message","Photo Text");
try {
$.ajax({
url:"https://graph.facebook.com/me/photos?access_token=" + authToken,
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 = atob(dataURI.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);
}
return new Blob([ab], { type: 'image/png' });
}
///////////////////////////////////////////////////////
/*
here i put my Access_Token got from:
https://developers.facebook.com/tools/explorer
is that the right access token code ??
*/
Access_Token = 'EAAM6K5d7vlsBAFMBcWpGcoznkNNMPtQWJ13sZBkWOJxgw51H0pgpfwyv06yIZC3qioJQ555z7KE12KB8tzimcfjHLB2FPosOalwXGqpiAzg3FKAsohqqKZAd3IGtDcVEIjNxOQsYR9LwKXckUIHW5fXuDENRgcecE6NdVLOCwUPZBZBhlkoGofCnetsyurj8NC2Ox9Jm2scwBJBpylhu4dNvZBFjedPoDrxNe8tZBjizAZDZD';
PostImageToFacebook(Access_Token);
</script>
</body>
</html>
ПРИМЕЧАНИЕ:
когда я открываю графическую ссылку, я получаю этот пустой json код:
{
"data": [
]
}
Нужна ваша помощь, пожалуйста ..
ОБНОВЛЕНИЕ 3:
я пробовал это решение здесь , но отображаю эту ошибку!
Invalid Scopes: publish_actions
мой полный код:
<html>
<head></head>
<body>
<button id="upload" type="button">Go Upload >></button>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'MY_AppId',
autoLogAppEvents : true,
xfbml : true,
status : true,
version : 'v6.0'
});
const dataURItoBlob = (dataURI) => {
let byteString = atob(dataURI.split(',')[1]);
let ab = new ArrayBuffer(byteString.length);
let ia = new Uint8Array(ab);
for (let i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], {
type: 'image/jpeg'
});
}
const upload = async (response) => {
// let canvas = document.getElementById('canvas');
// let dataURL = canvas.toDataURL('image/jpeg', 1.0);
let dataURL = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAIAAAAiOjnJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAMnSURBVHhe7doxa5VXHMDh++Y7CEXI5HIh3VxF6FAcMjt0U/AjCJ06OQmSxVWIS+mQJUsHJ5cMXcRJyCpKKDj4DfIa8VzaJL5SSn6Tz8OF+3/P/OO85x7u9PH94Qqu2tb4hislLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwS08f3h2P8zh39cfrw1ZjPrHe3nv805v/u973TpydjPrN7f+u3nTF/Z+xYJIRFQlgknLE2ruSMxYYdi4SwSHgVbiy9Ct+9mZ+/mP/cXCKsr08/35lu76y2x8I5rhs27Fjf8GF+tHd6d/+fqs4cn8xP90/v7s1HH8YKXyOsJX/P9x6fS+qck/nh4/loPHCZsBYcv5qPx7hkfvZyTFwirG9a35wOft3668nnz8H9aT2Wh+PX87sxcoGwln0+v/8ybV8bj9s706PdMQ8nq7dj4gJhLZoeXLog3f7x4qbFAmEtubm6NaZ/uba6MaYv5rd+G36dsBasf5jGxP8hLBLCIiEsEsIiISwSwiIhLBLCIiEsEsIiISwS/vNOwo5FQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVCWCSERUJYJIRFQlgkhEVgtfoEc2NhOeMjF60AAAAASUVORK5CYII=';
let blob = dataURItoBlob(dataURL);
let formData = new FormData();
formData.append('access_token', response.authResponse.accessToken);
formData.append('source', blob);
console.log('your access_token is: '+response.authResponse.accessToken);
let responseFB = await fetch('https://graph.facebook.com/me/photos', {
body: formData,
method: 'post'
});
responseFB = await responseFB.json();
console.log(responseFB);
};
document.getElementById('upload').addEventListener('click', () => {
FB.login((response) => {
//TODO check if user is logged in and authorized publish_actions
upload(response);
}, {scope: 'publish_actions'})
})
};
</script>
<script async defer src="https://connect.facebook.net/ar_EG/sdk.js"></script>
</body>
</html>
Сообщение об ошибке Facebook:
Invalid Scopes: publish_actions. This message is only shown to developers. Users of your app will ignore these permissions if present. Please read the documentation for valid permissions at: https://developers.facebook.com/docs/facebook-login/permissions
ошибка console.log:
index3.html:38 POST https://graph.facebook.com/me/photos 403
error: {message: "(#200) This endpoint is deprecated since the required permission publish_actions is deprecated", type: "OAuthException", code: 200, fbtrace_id: "AhLnAwIDnu0cbR7HjzV_YRF"}
Итак, кажется, что требуемое разрешение publish_actions устарело!
Теперь, как избежать этой проблемы?