I am trying to post to a users page . This is how far i have come , I get userId and userToken from FB.getLoginStatus and then post to my feed using this code . Now I want to post to a page but i keep getting you dont have permission to post to a page.
FB.api('/me/feed', 'post', {
message:'This is my message',
link:"http://trello.com",
name: 'This is my message',
description: 'This is my message'
},function(data) {
console.log(data);
alert(data);
});
},
{ scope: "publish_actions" });
You are mixing some things up. Permissions need to be asked for with FB.login
, not when using the API:
FB.login(function(response) {
if (response.authResponse) {
//user is logged in
}
}, {scope: 'publish_actions', return_scopes: true});
After authorization, you can use the API:
FB.api('/me/feed', 'post', {
message: 'This is my message',
link: 'http://trello.com',
name: 'This is my message',
description: 'This is my message'
}, function(data) {
console.log(data);
});
});
More information: http://www.devils-heaven.com/facebook-javascript-sdk-login/
That´s for the user profile. Posting to a Page (as Page) needs the manage_pages
and publish_pages
permissions:
FB.login(function(response) {
if (response.authResponse) {
//user is logged in
}
}, {scope: 'manage_pages,publish_pages', return_scopes: true});
You have to get a Page Token for your Page with this API call after authorizing: /{page-id}?fields=access_token
:
FB.api('/' + pageId + '', {fields: 'access_token'}, function(response) {
console.log(response);
});
After that, you can use the Token in the response to post as Page.
FB.api('/' + pageId + '/feed', 'post', {message: 'some message', access_token: pageToken}, function(response) {
console.log(response);
});
Side Note: Prefilling the message and autoposting (right after login) is not allowed, make sure you read the platform policy before creating any App: https://developers.facebook.com/policy/
You are using the wrong endpoint , you have to use '/pageid/feed'. After getting the userid and token you need to get all the pages he has permission for by hitting the '/me/accounts' . Now you get the pageid of page you want to post from here and then hit '/pageid/feed' to post . Here is some code:
FB.login(function(responses1) {
FB.api('/me/accounts', 'get',function(data) {
//you will get a list here.......//for now i am hardcoding it to the first page
var page_access_token = data.data[0].access_token;
var pageId = data.data[0].id;
//now post to the page
var target = '/'+pageId+'/feed'
FB.api(target,
'post',
{ message: "message",
link: "link",
picture: "",
caption: "",
description: "",
name: "",
access_token: page_access_token
}
,function(response) {
if (!response || response.error) {
console.log("in error");
console.log(response.error);
} else {
console.log(response);
}
});
});
}, { scope: "manage_pages,publish_pages" });
Also you need to change scope to :
manage_pages,publish_pages
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.