我知道这里有很多其他类似的请求 - 但它们都没有提供我能找到的解决方案。首先请不要指向我的PHP答案 - 我需要使用JavaScript SDK进行此实现,我希望用JavaScript解决它。
我正在尝试将照片发布到登录用户为管理员的Facebook页面。我得到了我认为的页面access_token,但它仍然将照片作为用户发布到页面而不是像页面管理员那样:
而不是这样:
我很确定我正在使用具有正确权限的页面access_token,您可以在Facebook的Access Token Debugger中看到:
所以这是我的代码:
<script>
var current_login_status = "start";
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : 'MY_APP_ID', // App ID from the app dashboard
status : true, // Check Facebook Login status
xfbml : true // Look for social plugins on the page
});
// Additional initialization code such as adding Event Listeners goes here
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated the app,
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
PostToFB(uid, accessToken);
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated the app
FB.login(function(response) {
current_login_status=response.status;
if (response.authResponse) {
window.location.reload(true);
} else {
alert('User did not fully authorize.');
}
}, {scope: 'publish_stream,manage_pages'});
} else {
// the user isn't logged in to Facebook.;
FB.login(function(response) {
current_login_status=response.status;
if (response.authResponse) {
window.location.reload(true);
} else {
alert('User cancelled login or did not fully authorize.');
}
}, {scope: 'publish_stream,manage_pages'});
}
});
};
function PostToFB(uid, accessToken) {
FB.api(
{
method: 'fql.query',
query: 'SELECT page_id, name, page_url FROM page WHERE page_id IN (SELECT page_id FROM page_admin WHERE uid='+ uid+')'
},
function(data) {
// Do something with the response
var page_id = data[0].page_id;
var page_accessToken = ''
FB.api(
'/'+uid+'/accounts',
function(returnData) {
page_accessToken = returnData.data[0].access_token;
}
);
FB.api(
'/'+page_id+'/photos',
'POST',
{
'access_token': page_accessToken,
'message': 'Beer Description',
'url': "IMAGE_URL"
},
function (response) {
if (response && !response.error) {
/* handle the result */
console.log(response);
} else {
console.log(response);
}
}
);
}
);
};
// Load the SDK asynchronously
(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/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
任何人都可以看到有什么问题吗?
答案 0 :(得分:0)
FB.api异步工作,因此您的第三个FB.api调用在page_accessToken
中没有设置任何值(因此使用了用户访问令牌)。
将第三个FB.api调用放入第二个调用的回调处理程序中 - 与第一个调用回调中的第二个调用相同。