2012-03-24 14 views
0

ich ein Beispiel lief kopiert aus:Facebook JS SDK liefert keine Ergebnisse

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

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

(ich meine APP_ID nur geändert)

<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> 

Nichts zeigt im Browser. Es gibt keine JavaScript-Fehler in der Konsole (Opera 11). Warum funktioniert es nicht? .

Antwort

0

Es sieht aus wie Sie nicht angemeldet sind Sie müssen die Benutzer authentifizieren zunächst ein OAuth-Token an Ort und Stelle zu bekommen, bevor Sie diesen Teil des Codes ausführen können:

 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); 
     } 
     } 
    }); 

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

1

Ich hatte das gleiche Problem und erkannte, dass das Beispiel die Antwort nicht korrekt analysiert. Das Objekt hat eine Eigenschaft Daten, die tatsächlich das zu analysierende Array ist.

Ein weiterer Punkt ist, dass Sie ein Token benötigen, um diese Operation auszuführen. So sollte Ihr Code sein:

<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> 
Verwandte Themen