问题
I've tried building the basis of our next app following Facebook's Open Graph Tutorial but for some reason I'm getting errors thrown at me regardless of how I try POSTing an action.
I'll go as in-depth as I can here as I hope it'll be very useful for all developers getting to grips with the new Timeline.
I've defined a battle action and a hero object using the default settings when setting up each. transcendgame is my namespace.
$id
is the userid of the Facebook user (I've tried using /me/, the direct id has always been less problematic in the past for me).
$herourl
is an urlencoded string pointing to a separate webpage with the following content:
<head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# transcendgame: http://ogp.me/ns/fb/transcendgame#">
<meta property="fb:app_id" content="[my id]">
<meta property="og:type" content="transcendgame:hero">
<meta property="og:url" content="http://apps.facebook.com/transcendgame/">
<meta property="og:title" content="Hercules">
<meta property="og:description" content="As this game is still in development, you can ignore this feed post!">
<meta property="og:image" content="[an image url]">
The application canvas page contains the following code. It should POST a new action event to the user's timeline, but is consistently giving me "Error occured". I have also tried it with the sample hero object (samples.ogp.me...) with the same problem.
<script>
function doTest()
{
FB.api('/<?=$id?>/transcendgame:battle' +
'?hero=<?=$herourl?>','post',
function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post was successful! Action ID: ' + response.id);
}
});
}
</script>
<a href="#" onclick="doTest(); return false">Test fb post</a>
I'm calling the JS SDK and running fb.init correctly. I honestly don't know where the problem even lies, let alone how to fix it.
EDIT: I've correctly added the user's access token to the API call:
FB.api('/me/transcendgame:battle' +
'?hero=<?=$herourl?>&access=<?=$token?>','post',
However, I'm getting the following error:
Type: OAuthException
Message: (#3502) Object at URL [url] has og:type of 'game'. The property 'hero' requires an object of og:type 'transcendgame:hero'.
This is odd, as the webpage definitely does have og:type set correctly, as listed earlier in this question. Is this some Facebook hiccup I need to work around?
SECOND EDIT: Fixed the final problem.
The og:url I had assumed to point back to the URL of the app's canvas, but it needs to reference itself instead. For instance, if your object is on mydomain.com/object1.php then the code must be:
<meta property="og:url" content="http://www.mydomain.com/object1.php">
Hope this helps others.
回答1:
I have the same problem and I changed the callback to be more helpful:
var fb_url = '/me/YOUR_NAMESPACE?key=value';
FB.api(fb_url,'post',
function(response) {
console.log(response);
if (!response || response.error) {
var msg = 'Error occured';
if (response.error) {
msg += "\n\nType: "+response.error.type+"\n\nMessage: "+response.error.message;
}
alert(msg);
} else {
alert('Post was successful! Action ID: ' + response.id);
}
}
);
UPDATE
I finally got a post to the timeline, and the change I had to make was to use the BETA version of JavaScript SDK. ( https://developers.facebook.com/support/beta-tier/ )
Use this:
<script src="//connect.beta.facebook.net/en_US/all.js#appId=xxx&xfbml=1"></script>
Instead of this:
<script src="//connect.facebook.net/en_US/all.js#appId=xxx&xfbml=1"></script>
回答2:
i had some difficultly posting initially as well. My problem lied the configuration settings of the action. Make sure that your actions are set to be for the exact object. Here are some screens of what I have set up and working:
If these settings are correct, check the error code that comes back from facebook, post it here and i can help you further.
回答3:
Regarding the final point on og:url -- that's correct. You can generally consider the OG URL to be the "primary key" of an Object in the Graph.
回答4:
To clear up some possible confusion, the og:url
doesn't "HAVE
" to be the same URL
as the url
of your object1.php
page. The important thing to note is that regardless of the URL
that you reference there, it has to have the META
tags in the html <head>
of the page.
I, for example, put the URL
of a canvas page in there so that when someone clicks the hyperlink in the action on the timeline it opens that page. As long as the META
information from the 'Get Code'
link for the Object is in the head, you won't get the error.
来源:https://stackoverflow.com/questions/7570335/difficulty-posting-actions-to-timeline