我正在尝试使用Graph API将画布图像分享到Facebook。我必须将其转换为Base 64图像并上传到Facebook blob存储。只有当我必须在代码中添加调试器并逐步调试它时,一切正常工作,但如果我直接点击Facebook共享按钮,那么这不会在Facebook上共享。请检查以下代码。
window.fbAsyncInit = function () {
FB.init({
appId: '338935439835732',
xfbml: true,
version: 'v2.8'
});
};
(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) { return; }
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
$("#btnfacebook").on('click', function () {
debugger;
var data = getCanvas.toDataURL("image/png").replace(/^data:image\/png/, "data:application/octet-stream");
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();
alert(auth.accessToken);
$.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' });
});
请帮我找到修复