Publsih action in Open Graph giving error - facebook

I am using Post action in Open graph using the below code, but getting the error message "[Object Object]"
What might be the problem???, i followed all the step by step guidance to publish an action from this URL
https://developers.facebook.com/docs/opengraph/tutorial/#publish
<script type="text/javascript">
function postArticle() {
FB.api(
'/me/ICONSolutions-test:Read',
'post',
{ Article: 'http://fbwerks.com:8000/zhen/cookie.html' },
function (response) {
if (!response || response.error) {
alert(response.error);
} else {
alert('Successful! Action ID: ' + response.id);
}
});
}
</script>
<input type="button" value="POST" onclick="postArticle()" />

You're alert()-ing an object which has no toString method that gives a readable representation of the object.
Dump the value to the console instead, console.log(response) – and have a look at it in Firebug or a similar debugging tool that implements console.

Related

How to get all survey answer by current user with REST Api Sharepoint?

In sharepoint survey API:
Get all question: https://site/_api/Web/Lists/getByTitle('Survey')/fields?$filter=(CanBeDeleted eq true)
Get all answer: https://site/_api/Web/Lists/getByTitle('Recognition%20Awards%202019')/items
Get all answer by current user login: ???
Please help me.
We can use _spPageContextInfo.userId to get the current login user Id, then using $filter=AuthorId eq UserId to get all the answer by current user login.
<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
function getItems() {
var listTitle="Recognition Awards 2019";
var currentUserId=_spPageContextInfo.userId;
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('"+listTitle+"')/items?$filter=AuthorId eq "+currentUserId,
type: "GET",
headers: { "ACCEPT": "application/json;odata=verbose" },
success: function (data) {
//
},
error: function (err) {
//alert(err);
}
});
}
</script>
<input id="Button1" type="button" value="Get Items" onclick="getItems()" />

Facebook Open Graph: How to Post User Message with Custom Action

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

How to find out who has published a post using my OG action

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.

FB.API 'Undefined' : news.reads activity

I'm trying to implement the Facebook news.reads App - I'm just missing one final step - executing the POST to FB. When I look at the Firebug console, it appears that the POST is in fact working...or at least there are no errors. Check out the output below:
Post:
__a 1
__user myID
app APPID
fb_dtsg AQBi1J4k
href http://www.around-around.com/today-show/
phstamp 1658166105497452107119
ref
Source
fb_dtsg=AQBi1J4k&href=http%3A%2F%2Fwww.around-around.com%2Ftoday-show%2F&app=183125741799131&ref=&__user=63802158&__a=1&phstamp=1658166105497452107119
(I've validated that the meta data from the URL is correct).
But the response I get from FB is strange:
for (;;);{"__ar":1,"payload":null}
No idea what to do with that. The referrer also seems to be the recommendation bar (which I'm using):
https://www.facebook.com/plugins/recommendations_bar.php?action=like&api_key=APPID&channel=http%3A%2F%2Fstatic.ak.facebook.com%2Fconnect%2Fxd_arbiter.php%3Fversion%3D11%23cb%3Df274495fd8bd4fc%26origin%3Dhttp%253A%252F%252Fwww.around-around.com%252Ff2b573e1404fec4%26domain%3Dwww.around-around.com%26relation%3Dparent.parent&href=http%3A%2F%2Fwww.around-around.com%2Ftoday-show%2F&locale=en_US&num_recommendations=2&read_time=30&sdk=joey&side=right&site=around-around.com&trigger=onvisible
So, I'm also using this code to try to automatically force a POST news.reads action.
<script type="text/javascript">
function readArticle()
{
FB.api(
'/me/news.reads',
'post',
{ article: '<?php the_permalink(); ?>' },
function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Article read was successful! Action ID: ' + response.id);
}
});
}
</script>
(you won't see this code on the site, as it's only firing for admins as of now)
I'm not getting any alerts so something is causing this to misfire...but using the recommendation bar would be preferable to us.
Anyways, any help would be appreciated.
Luke

Facebook Open Graph Action coding error

i've been figuring it out for ages, there's no problem debugging tool or in the terminal console, but it keeps popping "error occurred" in the following codes:
<script type="text/javascript">
function postCook()
{
$pageURL = window.location;
FB.api('/me/bgfapp:watch?movie=' + $pageURL,'post',
function(response) {
if (!response || response.error) {
alert('Error Occurred');
} else {
alert('Post was successful! Action ID: ' + response.id);
}
});
}
</script>
i tried to echo $pageURL and it returns the current URL successfully, so i can't figure out what's wrong with the above code
updated: 30-Jan-2012
the error says: Error Occurred[object Object][object Object]
You've probably already found a solution but hopefully this will help someone else.
The problem here is that you don't know the names of the child nodes within the response object. If you can't name the specific node then you're going to continually get that error message: "Error occured [Object object] message"
I have a simple workaround that will allow you to see the error message without knowing the names of the response object's child nodes. JSON.stringify will simply convert the entire object into a string, allowing you to view its contents. It won't be pretty but you'll definitely be able to see the error message in there.
Try this:
<script type="text/javascript">
function postCook()
{
$pageURL = window.location;
FB.api('/me/bgfapp:watch?movie=' + $pageURL,'post', function(response) {
if (!response || response.error) {
alert(JSON.stringify(response));
}
else {
alert('Post was successful! Action ID: ' + response.id);
}
});
}
An alternate solution would be to output the contents of the response object to the console rather than executing an alert, just replace line 7 in my code sample with this:
console.log(response);
From here, you can open the development console of your web browser and traverse the contents of the response object. Since the console is not always available (phonegap apps for example), the former solution is sometimes more suitable.
Based on your new error message it looks like you need to see what response.error says. Your logic says that either you got no response at all or you got a response.error. You should first figure out what case you are in and act accordingly.
response.responseText and response.error.responseText are undefined because they aren't returned to you.
$pageURL = '/me/bgfapp:watch?movie=' + window.location;
FB.api($pageURL,'post',
function(response) {
if (!response) {
alert('Error Occurred I got no response with ' + $pageURL);
}
else if (response.error) {
alert('Error Occurred '+ response.error);
} else {
alert('Post was successful! Action ID: ' + response.id);
}
});
My suggestion is try simple and work your way up. Debug all variables that you are checking. If you aren't getting a response it could be that your API endpoint call doesn't exist. If you are getting an error then your call is wrong or maybe not authenticated.