Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

javascript - Difficulty posting Actions to Timeline

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 += "

Type: "+response.error.type+"

Message: "+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&amp;xfbml=1"></script>

Instead of this:

<script src="//connect.facebook.net/en_US/all.js#appId=xxx&amp;xfbml=1"></script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...