Facebook Open Graph Action coding error - facebook

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.

Related

How to display a the name of the user in a Facebook post

I have this function which works fine :
function testAPI() {
FB.login(function(){
FB.api('/me/feed', 'post', {message: 'I am going for a run.'},
function(response){if (!response || response.error)
{
document.getElementById('status').innerHTML = 'Error occured';
}
else
{
document.getElementById('status').innerHTML = 'ok';
}
}
);
}, {scope: 'publish_actions'});
}
This posts a facebook post like this as you can see:
I am going for a run.
What I want to do is if the user is Mike Tyson, I want to post :
Mike Tyson is going for a run.
I did {message: '{name} is going for a run.'}
But it didn't work.
Is there an easy way to do this? Thanks.
"it is working" does not mean it´s allowed. You are not allowed to prefill the message parameter, it MUST be 100% user generated: https://developers.facebook.com/policy/ (2.3)
What you would want to use is called "Custom Stories": https://developers.facebook.com/docs/opengraph

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

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

Publsih action in Open Graph giving error

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.

Sencha Touch and Facebook button

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()).