Am working on a facebook application to post images to a page, i want to delete a particular the facebook post using javascript api, any help please?
This is my code:
FB.api('/1234/photos', 'post', {
access_token : FB.getAuthResponse()['accessToken'],
message: "message",
url : '',
from : 1234,
}, function (response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
Delete post function:
function delete_post(post_id)
{
FB.api(post_id, 'delete',function(response) {
console.log(response)
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post was deleted');
}
});
}
This is not working for me, always getting error occured message.
which Id should i choose to delete a post? response.id or response.post_id?
Thanks
Related
I am trying to post to my facebook's wall using javascript SDK. I am first getting user access token. Then using user access token i am getting page access token and including it while posting to a wall. I'm getting following error
here's my code : First am trying to login using FB.login
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.'+JSON.stringify(response));
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
},{scope: 'manage_pages,publish_pages'});
Then trying to post to a page.
var body = 'Reading JS SDK documentation';
FB.getLoginStatus(function(response) {
console.log('login status',response);
if(!(response.status === 'connected')){
location.href = './fb-login.html';
} else {
uID = response.authResponse.userID;
accessToken = response.authResponse.accessToken;
console.log('accesstoken::',response.authResponse.accessToken);
FB.api('/me', {fields: 'last_name'}, { access_token : accessToken } ,function(response) {
console.log(response);
});
//get list of pages
if(accessToken!=null){
FB.api('/me/accounts','get',{ access_token : accessToken },function(response){
console.log('resp of pages',response);
if(response!=null){
var data = response.data;
pageAccessToken= data[0].access_token;
console.log('pageAccessToken::',pageAccessToken);
FB.api('/6599048*******/feed', 'post', {message :body, access_token : pageAccessToken }, function(response) {
console.log('response',response)
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
}
});
}
I had successfully post a message to my own Facebook page but it was posted as a visitor(I want post as a page). I'm not sure where i missed and also i confused to the page access token.
$('#btn-fb').click(function () {
getPageAccess(function (page_id){
postToFB(page_id,'sien');
});
//postStatus();
});
function getPageAccess(callback) {
FB.login(function (response) {
// handle the response
if (response.authResponse) {
FB.api('/' + page_id + '', {fields: 'access_token'}, function (response) {
if (response && !response.error) {
callback(page_id);
}
});
}
}, {scope: 'manage_pages,publish_pages'});
}
function postToFB(page_id, message) {
FB.api(
"/" + page_id + "/feed",
"post",
{
"message": message
},
function (response) {
if (response && !response.error) {
/* handle the result */
}
else {
}
}
);
}
You need to use a Page Token to post as Page, right now you are using a User Token. You are asking for the Page Token (fields parameter), but you never use it and you don´t add it as callback parameter:
{
'message': message,
'access_token': pageToken
}
I want my my postuserWall() function to wait for the FB.api response then it return true or false.but it is not waiting for the response.
How can i get it to wait for the response
here is my main function which is calling the postuserWall function the alert is showing me undefined
$('.vacancies_tbl').on('click', '.btn-edit-vacancy', function(e) {
e.preventDefault();
var $this = $(this);
var chcek = postuserWall();
alert(chcek);
if(check){
window.location = $this.attr('href');
}
});
Here is my postuserWall function
function postuserWall(){
var body = 'Usama New Post';
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
console.log(response);
alert('Error occured');
return false;
} else {
alert('Post ID: ' + response.id);
return true;
}
});
}
it is not waiting for the response in the postuserWall function
One solution (make sure you understand what asynchronous means, it´s very important in JavaScript):
$('.vacancies_tbl').on('click', '.btn-edit-vacancy', function(e) {
e.preventDefault();
var $this = $(this);
postUserWall(function(check) {
alert(check);
if(check){
window.location = $this.attr('href');
}
});
});
function postUserWall(callback) {
var body = 'Usama New Post';
FB.api('/me/feed', 'post', {message: body}, function(response) {
if (!response || response.error) {
console.log(response);
alert('Error occured');
callback(false);
} else {
alert('Post ID: ' + response.id);
callback(true);
}
});
}
You can also use Promises, but that´s overkill for just one callback.
I'm using the following code to delete a collection in my db:
Client:
$('.destroy').click(function() {
if(confirm("Are u sure?")) {
$.ajax({
type: 'DELETE',
url: '/destroy/' + dataId,
success: function(response) {
console.log('Success');
}
});
} else {
alert('Cancelled');
}
});
Server:
app.get('/destroy/:id', function(req, res) {
var id = req.param("id");
MyModel.remove({
_id: id
}, function(err){
if (err) {
console.log(err)
}
else {
console.log('Collection removed!');
}
});
});
Is working, if i click in the destroy button and reload the page, the collection will not be there, but the success callback function with the: [console.log('Success');] is not running..
I need send a callback from the server to the client ir order to make the success function run???
How make the console.log('Success'); run??
Thanks.
The ajax call probably just times out, as it's never getting a response back from the server.
Send a response from the server
app.get('/destroy/:id', function(req, res) {
var id = req.param("id");
MyModel.remove({
_id: id
}, function(err){
if (err) {
res.end('error');
}
else {
res.end('success');
}
});
});
Then catch it
$.ajax({
type : 'DELETE',
url : '/destroy/' + dataId,
success : function(response) {
if ( response === 'error' ) {
alert('crap!');
} else if (response === 'success' ) {
alert('worked fine!');
}
}
});
This is a simplified example, you can return whatever you like, send statusCodes or whatever.
When I try to perform an action with JS FB.api([...], I get No callback passed to the ApiClient [...]
Where can I set this callback?
Just add a callback function to the API call:
FB.api(
'/me/[YOUR_APP_NAMESPACE]:[YOUR_ACTION]',
'post',
{ recipe: '[LINK_TO_YOUR_OBJECT]' },
function(response) {
if (!response || response.error) {
alert('Error occured');
}
else {
alert('Action was successful! Action ID: ' + response.id)
}
}
);