The Facebook tutorials suggest that you can add a user comment to a custom action in a Facebook App. The example javascript function for posting is:
<script type="text/javascript">
function postCook()
{
FB.api(
'/me/[YOUR_APP_NAMESPACE]:cook',
'post',
{ recipe: 'http://fbwerks.com:8000/zhen/cookie.html' },
function(response) {
if (!response || response.error) {
alert('Error occurred');
} else {
alert('Cook was successful! Action ID: ' + response.id);
}
});
}
</script>
I have an "Endorse" action defined with the object "Local Business". Everything is working. Now I want to give the user the option to add a user message to their endorsement but I can not find any help in Facebook docs on how to implement this in the api (the above code). Any help?
You need to specify the 'message' parameter when publishing the action. For example,
<script type="text/javascript">
function postCook(userMessage)
{
FB.api(
'/me/[YOUR_APP_NAMESPACE]:cook',
'post',
{ recipe: 'http://fbwerks.com:8000/zhen/cookie.html',
message: userMessage },
function(response) {
if (!response || response.error) {
alert('Error occurred');
} else {
alert('Cook was successful! Action ID: ' + response.id);
}
});
}
</script>
will submit userMessage as a user provided message on the action.
The full list of supported parameters for the OpenGraph publishing API is available here: https://developers.facebook.com/docs/technical-guides/opengraph/publish-action/#create
Related
I'm using the Facebook JS SDK to publish to the GRAPH API, and while my post publishes successfully, it returns a strange response object that gets caught by the jQuery .error chain. Here's my code:
var jqxhr = $.post("https://graph.facebook.com/"+sbFacebook.authentication.userID+"/feed",
{
'message' : $("#question-text").val(),
'access_token' : sbFacebook.authentication.accessToken
}).success(
function(response) {
console.log(response);
})
.error(function(response) {
alert(response);
})
The response object looks like this:
http://img689.imageshack.us/img689/964/screenshotfrom201302222.png
Anyone have any idea what's going on and why it's getting caught by the error() function?
Thanks.
This is not how you should use Facebook JS SDK.
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);
}
});
Don't forget to initialize the JS SDK:
Hoping someone can point me in the right direction.
I have an open graph action that has finally been approved and is active on my app now. Is there any way to track who publishes a post using my OG action from within my app?
Here is the code I'm using for my action:
<script type="text/javascript">
function postEndorse()
{
FB.api(
'/me/namespace:endorse',
'post',
{ photo: '<?php the_permalink(); ?>' },
function(response) {
if (!response || response.error) {
alert('Error Occurred' + response + " " + response.error);
} else {
alert('Thank you!');
}
});
}
</script>
I am requesting the publish_actions permissions when people log in.
Thanks
The response in your JavaScript code above has the id of the action.
{
id: “{action-instance-id}”
}
You need to store your actions via database or otherwise. Once you have that you can make a call against it
/action-instance-id?fields=from.fields(id)
Which will give a response such as
{
"from": {
"id": "{user-id}"
},
"id": "{action-instance-id}"
}
There are many ways to store this data, one way would be AJAX to PHP/MySQL.
I suggest reading up on it. The following hasn't been tested, just gives an idea of how to send the data across. Think of it as pseudocode
Add jQuery to the <head>
<script src="jquery.js"></script>
Send the id of the user, like
<script type="text/javascript">
function postEndorse()
{
FB.api(
'/me/namespace:endorse',
'post',
{ photo: '<?php the_permalink(); ?>' },
function(response) {
if (!response || response.error) {
alert('Error Occurred' + response + " " + response.error);
} else {
FB.api(
response.id + "?fields=from.fields(id)",
function(resp) {
if (!resp || resp.error) {
alert('Error Occurred' + resp + " " + resp.error);
} else {
$.ajax({
type: "POST",
url: "fbdata.php",
data: { id: resp.from.id, actionid: resp.id }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
}
});
}
});
}
</script>
Then in fbdata.php something like
<?php
include("database.inc.php");
mysql_connect($server,$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$id = mysql_real_escape_string($_POST['id']);
$actionid = mysql_real_escape_string($_POST['actionid']);
$database_entry = "INSERT INTO Actions (Id, Action) VALUES ('$id', '$actionid') ;
mysql_query($database_ntry) or die(mysql_error());
mysql_close();
?>
References that you should read
http://api.jquery.com/jQuery.ajax/
http://www.php.net/manual/en/mysql.examples-basic.php
What about if you change
/me/namespace:endorse to /users facebook id/namespace:endorse Then you can store the facebook id of the person and then track them back using that.
hi,
Using Facebook's API it's possible to prompt a user to post to his/her wall using the feed dialog. However, using the javascript SDK, this requires two clicks: one on the button which brings up the dialog, the other on the "Share" button within the dialog.
Is it possible to get rid of one of these clicks? I thought of two approaches:
Embedding the dialog within an iframe, as Facebook provides a URL for a full page dialog. That will require the user to click only on the "Share" button. Apparently Facebook blocks that option.
Using an access token and display=iframe, but I want to avoid the user having to authorize my application.
Any ideas?
i would suggest you to use fb.api instead.
function PostToMyWall(){
FB.login(function(response)
{
if (response.authResponse)
{
//Post to my wall
FB.api('/me/feed', 'post', {
message: lnk,link: lnk
},
function(response) {
if (!response || response.error) {
// //alert('Error occured');
} else {
// //alert("Successfully Posted to My Wall!");
}
});}else
{
alert('Not logged in');
}}, { scope : 'publish_stream' });
}
My application got a similar function, check it out at www.wootube.woolei.com - "Post to WooTube Page and Group"
or popup when the page using jquery
$(document).ready(function() { FB.ui({method: 'feed',
name: mdtxt,
link: lnk,
picture: hackImageUrl(img,fld),
caption: '<?php echo $clickme; ?>',
description: '<?php echo $app_desc; ?>'
},
function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert("Successfully Posted to Group");
}
});});
I need to create a button to submit a comment on Face Book wall.
I am using MVC in Sencha touch and in my controller I use a function as
facebookComment: function()
{
--------
}
This function is to be called when a button is pressed.
Can any body please throw some light on how to go about this?
i am using following code to post on friends wall see if it is useful to you or not
var postParams = {
method: 'stream.publish'
, target_id: friend_id
, display: 'popup'
, message: message
, user_message_prompt: "Post message!"
}
FB.api(postParams, function(response) {
if(response.error_code || !response) {
to handle error
}
});
or refer this link https://developers.facebook.com/docs/reference/rest/stream.publish/
Well, first you should already be using the Javascript SDK. Then just issue a HTTP POST request to the feed connection of the current user using the FB.api() method with the message field set to your comment:
var body = comment_var;
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
Obviously, should be taking care of the user login status (maybe using the method FB.getLoginStatus()).
I have a small script that unlocks some content on a page when a user logs in via Facebook and posts some information on their wall.
I want to avoid any duplicate posts (spam) in the event the user visits the page a second time and repeats the process.
Is there a way of (when asking for the permissions) to tell if they have already been given and therefore skip the sharing part of the script?
Here's my code:
$('.preview a').click(function(e) {
e.preventDefault();
FB.login(function(response) {
if (response.session) {
FB.api('/me/feed', 'post', {
message: 'Just unlocked the exclusive preview of...',
picture: 'http://website.com/share.png',
link: 'http://www.website.com',
name: 'Name goes here',
description: 'Description here'
},
function(response) {
if (!response || response.error) {
alert(response.error);
} else {
$('.preview').hide();
$('.clip').show();
}
});
}
},
{perms:'read_stream,publish_stream'});
});
From 2011 to 2014 a lot of things change..
I made this solution to check for "user_friends" and "publish_actions" permissions, if not allowed the both, force the user to "re-autenticate". The callback method will only be called when all permissions are given.
function login(cb,forceAuth) {
FB.getLoginStatus(function (response) {
if (response.status !== 'connected' || forceAuth==true){
FB.login(function (response) {
checkPermissions(cb,false);
}, {scope: 'publish_actions,user_friends'});
} else {
checkPermissions(cb);
}
});
}
function checkPermissions(cb,forceAuth){
FB.api(
"/me/permissions",
function (response) {
if (response.data[0]['publish_actions']==undefined || response.data[0]['publish_actions']==0 ||
response.data[0]['user_friends']==undefined || response.data[0]['user_friends']==0) {
if (forceAuth!=false)
login(cb,true);
} else {
cb();
}
}
);
}
How to use:
login(function () {
myLogedAndAllowedFunction();
});
I've written a tutorial about this here, and here's an example:
FB.api({ method: 'fql.query', query: 'SELECT read_stream,offline_access,publish_stream FROM permissions WHERE uid=me()' }, function(resp) {
for(var key in resp[0]) {
if(resp[0][key] === "1")
console.log(key+' is granted')
else
console.log(key+' is not granted')
}
});
There's also a JS REST example there in the article just for reference.