Facebook Post image to Facebook fan page with Javascript - javascript

It's funny really...
This is my script
var imgURL = "http://onseiekoerant.com/upload/uploads/1411504661_2786.jpg";
FB.api('/'MY PAGE ID'/photos', 'post', {
message: 'Website Genius',
url: imgURL,
access_token: accessToken
}, function (response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
//alert('Post ID: ' + response.post_id);
}
console.log(response);
});
When I call this function, it returns te restponse the the POST_ID. It's suposed to be successfull, but nothing is on my facebook fan page.
To give additional info, the access token cames from my currently signed in user of facebook, which is admin of the page.
What am I missing?

Related

Problems with FB.api() posting and sending notifications

I cant seem to make a POST request or get this function to work, but I cant seem to do it and it gave no specific error. But here is the code
var body = 'Hello world on post';
FB.api('/100008601850848/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
alert('Error occured' + response.error);
} else {
alert('Post ID: ' + response.id);
}
});
I dont have any information to give you because the error did not give me information and FB api documentation on it is not clear

adding Facebook friends to a friendlist with FB.api

How can I add friend(s) to a friendlist using JavaScript SDK?
I found this example for using POST method in order to post the message to the feed:
var body = 'Reading JS SDK documentation';
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
However, I do not understand, how it works. Ehat is 'message' in { message: body } and how it can be adapted to this matrix "/FRIENDLIST_ID/members?members=1,2,3" (this is from FB.api examples ar https://developers.facebook.com/docs/reference/javascript/FB.api/
This is what worked for me. Make sure the key is enclosed within quotes. 'members' in this case. Though I was able to create lists, trying to add members to a created list kept throwing an exception with a message saying 'Manage_friendlists permission required' thought I had that permission. Turns out one needs the publish stream permission as well in order for this to work. This wasn't mentioned anywhere. Hope this helps.
FB.api('/' + friendlistID + '/members', 'post', {'members': members}, function(response) {
console.log(response);
if (!response || response.error) {
alert('Could not save members!');
} else {
alert('Members added to list!');
}
});
Simply change the endpoint, and the data you're sending.
The Graph Endpoint you're using is FriendList
friendlist_id = 1435134523451;
FB.api('/'friendlist_id+'/members', 'post', {members:'comma,separated,ids,of,friends,to,add,to,list'}, function(response) {
console.log(response);
if (!response || response.error) {
alert('Error occured');
} else {
alert('yay!');
}
});
I think
FB.api('/FRIENDLIST_ID/members?members=1,2,3', 'post', function(response) { … });
should do it.

FB API posting to feed

I have been stuck on this for a while.
I have the FB SDK working perfectly for everything; logging in, logging out, getting user pictures, etc. But I cant figure out this simple problem.
Why is this code alerting 'Error Occurred' and not what I want it to.
Post to Facebook
<script>
function postToFacebook() {
FB.api('/me/feed', 'post', { message: 'Test Message' }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response);
}
});
}
</script>
Thank you in advance!

How to publish something on user's friend stream/wall in an application

In a Facebook application, I need to publish a user's friend stream a mlessage.
how can i figure out of that?
Thanks
send HTTP POST request to the following address https://graph.facebook.com/FRIEND_ID/feed
it returns the ID of posted message on success
here's the example code:
var msg = 'hello world';
FB.api('/YOUR_FRIEND_ID/feed', 'post', { message: msg }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
You can also use the javascript API for posting. The manual has sample code.
https://developers.facebook.com/docs/reference/javascript/FB.ui/
FB.ui(
{
method: 'feed',
to: friendId,
name: 'title',
link: 'http://host.com/title_link.com',
picture: 'http://host.com/image.jpg',
description: 'description',
caption: 'caption',
},
function(response) {
// Check for a posting to wall
if (response && response.post_id) {
// do some logging
}
}
});

How to post to Facebook fanpage as fanpage, not user

How can I post on fanpage wall as fanpage not as a user - using javascript sdk.
RIght now on Init im receiving menage_pages and acquiring suitable fanpage id, how can i change call below?
var target = '/'+params.target+'/feed'
FB.api(target,
'post',
{ message: params.message,
link: params.link,
picture: params.picture,
caption: params.caption,
description: params.description,
name: params.name
}
,function(response) {
if (!response || response.error) {
$("#error").removeClass('hidden');
} else {
$("#success").removeClass('hidden');
}
});
You need the following permissions:
publish_stream
manage_pages
Now we call the page object to retrieve the page's access_token and then post with that token, something like:
function postToPage() {
var page_id = 'MY_PAGE_ID';
FB.api('/' + page_id, {fields: 'access_token'}, function(resp) {
if(resp.access_token) {
FB.api('/' + page_id + '/feed',
'post',
{ message: "I'm a Page!", access_token: resp.access_token }
,function(response) {
console.log(response);
});
}
});
}
Result:
More about this in my tutorial.
All you need is those three functionalities to be implemented:
First one is for setting the basic parameters for your Application(XXX-APP needs to be changed with your real id of application).
Second, function fol login to Facebook and granting needed permissions to be able to post to Facebook.
Third function is for posting to Facebook(XXX-page need to be changed with ID of the Page that you want to post something)
FB.init({ appId: 'XXX-APP', status: true, cookie: true, xfbml: true, oauth: true });
access_token = '';
function loginFB(){
FB.login(function(response) {
if (response.authResponse) {
access_token = FB.getAuthResponse()['accessToken'];
console.log('Access Token = '+ access_token);
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, {scope: 'publish_stream,manage_pages'});
}
function postToPage() {
FB.api('/XXX-page', {fields: 'access_token'}, function(resp) {
if(resp.access_token) {
FB.api('/' + page_id + '/feed',
'post',
{ message: "MSG", access_token: resp.access_token }
,function(response) {
console.log(response);
});
}
});
}

Categories

Resources