Provided that when the user connected / logged in to your app/game you have asked for the correct permissions it should be pretty easy. Below is some code I wrote for a project of mine. The user would have had to specifically allowed the app to publish to the stream on their behalf.
Pay special attention to the :
{scope: 'publish_stream'}
This will ask the user during the connection phase if it's ok for your game/app to publish to the stream on the users behalf.
// Click Handler for the login.
$(document).delegate('#fb-connect', 'click', function(e){
e.preventDefault();
FB.login(function(response) {
// Check the status
// console.log(response.status);
if (response.status === 'connected') {
// Run the function we created below to publish to the users stream.
publish('I just logged in via Facebook. '); // Your Message
} else if (response.status === 'unknown' || response.status === 'not_authorized') {
// Something else if they chose not to connect
} else {
}
}, {scope: 'publish_stream'});
});
This is a simple function that you can pass a string to (your message) and will post to the currently connected users stream.
function publish(messagebody) {
var body = messagebody; // This is the passed in string aka message you want to post.
params = {
message: body, // Your message body
link: 'http://www.yoursite.com' // if you want to share a link.
}
FB.api('/me/feed', 'post', params, function(response) {
if (!response || response.error) {
//alert('Error occured');
console.log(response.error);
} else {
// Successful post to facebook.
// alert('Post ID: ' + response.id);
}
});
}
Hope this helps you!
EDIT: Adding FULL HTML Example page.
<html>
<head>
<title>Your Page</title>
<!-- Load the Facebook SDK for your APP -->
<script>
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : 'YOURAPPID', // Test App ID for localhost
channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel file for x-domain comms
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
};
// 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>
</head>
<body>
<!-- Markup for Connect Button -->
<a id="fb-connect">
<img src="URLTOANIMAGEYOULIKEFORTHEBUTTON">
</a>
<!-- Reference to Jquery -->
<script src="PATHTOJQUERY"></script>
<!-- Some Additional Script to handle the functions / Could also be another JS file you include. -->
<script>
$(document).ready(function(){
// Bind the Click of the Button fb-connect
$(document).delegate('#fb-connect', 'click', function(e){
e.preventDefault();
FB.login(function(response) {
// Check the status
// console.log(response.status);
if (response.status === 'connected') {
// Run the function we created below to publish to the users stream.
publish('I just logged in via Facebook. '); // Your Message
} else if (response.status === 'unknown' || response.status === 'not_authorized') {
// Something else if they chose not to connect
} else {
}
}, {scope: 'publish_stream'});
});
});
function publish(messagebody) {
var body = messagebody; // This is the passed in string aka message you want to post.
params = {
message: body, // Your message body
link: 'http://www.yoursite.com' // if you want to share a link.
}
FB.api('/me/feed', 'post', params, function(response) {
if (!response || response.error) {
//alert('Error occured');
console.log(response.error);
} else {
// Successful post to facebook.
// alert('Post ID: ' + response.id);
}
});
}
</script>
</body>
</html>