2012-03-24 12 views
0

を返していない私はからコピーされた例を実行しました:FacebookのJS SDKは何の結果

http://developers.facebook.com/docs/reference/javascript/

http://developers.facebook.com/docs/reference/javascript/FB.api/

(私は私のAPP_IDを変更)

<div id="fb-root"></div> 
<script> 
    window.fbAsyncInit = function() { 
    FB.init({ 
     appId  : 'MY_ID', // App ID 
     channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File 
     status  : true, // check login status 
     cookie  : true, // enable cookies to allow the server to access the session 
     xfbml  : true // parse XFBML 
    }); 

    FB.api('/platform/posts', { limit: 3 }, function(response) { 
    for (var i=0, l=response.length; i<l; i++) { 
    var post = response[i]; 
    if (post.message) { 
     alert('Message: ' + post.message); 
    } else if (post.attachment && post.attachment.name) { 
     alert('Attachment: ' + post.attachment.name); 
    } 
    } 
}); 
    }; 

    // Load the SDK Asynchronously 
    (function(d){ 
    var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; 
    if (d.getElementById(id)) {return;} 
    js = d.createElement('script'); js.id = id; js.async = true; 
    js.src = "//connect.facebook.net/en_US/all.js"; 
    ref.parentNode.insertBefore(js, ref); 
    }(document)); 
</script> 

何もブラウザに示していません。コンソールにはJavaScriptエラーはありません(Opera 11)。それはなぜ機能しないのですか?

答えて

0

あなたはログインしていないように見えますが、あなたがこのコードの一部を実行する前に、最初の場所でのOAuthトークンを取得するには、ユーザーを認証する必要があります。

 FB.api('/platform/posts', { limit: 3 }, function(response) { 
     for (var i=0, l=response.length; i<l; i++) { 
     var post = response[i]; 
     if (post.message) { 
      alert('Message: ' + post.message); 
     } else if (post.attachment && post.attachment.name) { 
      alert('Attachment: ' + post.attachment.name); 
     } 
     } 
    }); 

参照:http://developers.facebook.com/docs/reference/javascript/FB.login/

1

私は同じ問題を抱えており、この例では応答を正しく解析していないことに気付きました。オブジェクトのプロパティはデータであり、実際には解析対象の配列です。

この操作を行うにはトークンが必要です。したがって、コードは次のようになります:

<div id="fb-root"></div> 
<script> 
    var fbToken; // Highly recommended to make it global 
    window.fbAsyncInit = function() 
    { 
     FB.init({ 
      appId  : 'MY_ID', // App ID 
      channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File 
      status  : true, // check login status 
      cookie  : true, // enable cookies to allow the server to access the session 
      xfbml  : true // parse XFBML 
     }); 

     FB.login(function(response) 
     { 
      if (response.authResponse) 
      { 
       fbToken = response.authResponse.accessToken; // Save it for another requests 
       FB.api('/platform/posts', {limit:3}, function(response){ 
        for (var i=0, l=response.data.length; i<l; i++) 
        { 
         var post = response.data[i]; 
         if (post.message) 
         { 
          alert('Message: ' + post.message); 
         } else if (post.attachment && post.attachment.name) 
         { 
          alert('Attachment: ' + post.attachment.name); 
         } 
         } 
       }); 
      } else { 
       // User did not accept oAuth 
      } 
     }); 
    } 
    // Load the SDK Asynchronously 
    (function(d){ 
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; 
     if (d.getElementById(id)) {return;} 
     js = d.createElement('script'); js.id = id; js.async = true; 
     js.src = "//connect.facebook.net/en_US/all.js"; 
     ref.parentNode.insertBefore(js, ref); 
    }(document)); 
</script> 
関連する問題