Hey guys am new to javascript facebook sdk .When i learn about the params in javascript i feel so confused.My code is
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);
}
});
This code works well ..But my doubt is that can we use the code like
var body = 'Reading JS SDK documentation';
FB.api('/me/feed?message=body', 'post', function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
Is there any problem with the second type of code ..Can it be done in Fb js sdk ..
Hope you guys can help me ..Thanx in advance
It works.. But Let me explain this a bit.
FB.api() method is declared as follows
FB.api(path, method, params, callback)
The method parameter given here is "post" which is equivalent to an html post.
As per the api documentation posting something to feed should be done with post method.
For a post method, to be on the safe side pass parameters through params
Lastly some javascript syntax. Here body is a variable so it should be FB.api('/me/feed?message='+body,... or the text body will be posted to your feed
Try out your code in the Test Console
See documentation for more details
Related
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
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.
I'm trying to post to user stream, without user prompt. I cannot manage to find a code that works. Facebook JSDK is already loaded and I will insert the code inside:
FB.getLoginStatus(function(response){
to make sure the user is already logged to my application. Could you provide an example of publishing to the user stream using the publish_stream permission?
With Dialog
You need to use the Feed Dialog, with FB.ui():
function postToFeed() {
// calling the API ...
var obj = {
method: 'feed',
link: 'https://developers.facebook.com/docs/reference/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
name: 'Facebook Dialogs',
caption: 'Reference Documentation',
description: 'Using Dialogs to interact with users.'
};
function callback(response) {
document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
}
FB.ui(obj, callback);
}
Documentation: https://developers.facebook.com/docs/reference/dialogs/feed/
Without Dialog
To make a post without the Dialog you need to use the FB.api():
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);
}
});
Documentation: https://developers.facebook.com/docs/reference/javascript/FB.api/
Direct URL
https://www.facebook.com/dialog/feed?
app_id=APP_ID&
link=https://YOUR_DOMAIN&
picture=http://YOUR_DOMAIN/image.jpg&
name=Facebook%20Dialogs&
caption=API%20Dialogs&
description=Using%20Dialogs%20to%20interact%20with%20users.&
redirect_uri=http://YOUR_DOMAIN/response
Documentation: https://developers.facebook.com/docs/reference/dialogs/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!
I know this question has been asked a thousand time, but I have yet to come across a definitive answer.
How does an application (external website, so no fbml) post to a users wall, using JS and Graph API?
I have established extended permissions with users, and can post to walls using Facebooks sample code, however the variables within the script wont appear on FB.
For Example:
function publish_test(){
var body = 'Reading Connect JS documentation';
FB.api('/me/feed', 'post', { body: body }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response);
}
});
}
This posts to the users wall, however it does not contain the body. Any help on this would be gratefully appreciated.
FB.ui will pop up a confirmation dialog to the user, not sure if you want that or not.
http://developers.facebook.com/docs/reference/javascript/FB.ui
This page has documentation about the FB.api call:
http://developers.facebook.com/docs/api
Scroll down to the "Publishing" section and notice that there's no "body" parameter specified. There is a "message" parameter though. The other example you're looking at is probably incorrect?
I got this code working
function graphStreamPublish(){
var body = document.getElementById("txtTextToPublish").value;
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
}