6

Nel mio codice hoJavaScript SDK Un token di accesso attivo deve essere usato per interrogare le informazioni sull'utente corrente

FB.api('/me/friends', function(response) { 
      if(response.data) { 
       //TODO : what to do if no. of friends is more than 5000 (pagination by fb) 
       friends_data=response.data; 
       dijit.registry.byId("mainWidget_div").set_friends_data(friends_data); 
      } else { 
       alert("Error!"); 
      } 

      }); 

e questo dà un errore. Ma, se chiamo questa funzione manualmente (sulla console), non c'è nessun errore

FB.api('/me/friends', function(response){r=response;}); 
//wait a while 
r 

e ora r.data è una serie di miei amici.

Ho controllato il pannello di rete e ho rilevato che quando lo chiamo manualmente, un token di accesso viene automaticamente inserito nell'URL della richiesta e quando viene chiamato tramite il codice, il token di accesso non viene inserito.

Il fb SDK codice di carico pieno nella mia domanda è questa:

<script type="text/javascript"> 
     // You probably don't want to use globals, but this is just example code 
     var fbAppId = "{{facebook_app_id}}"; 

     // This is boilerplate code that is used to initialize the Facebook 
     // JS SDK. You would normally set your App ID in this code. 

     // Additional JS functions here 
     window.fbAsyncInit = function() { 
     FB.init({ 
      appId  : fbAppId,  // App ID 
      status  : true,   // check login status 
      cookie  : true,   // enable cookies to allow the server to access the session 
      xfbml  : true   // parse page for xfbml or html5 social plugins like login button below 
     }); 

     // Put additional init code here 
     dojo.ready(function(){ 
      FB.api('/me/friends', function(response) { 
      if(response.data) { 
       //TODO : what to do if no. of friends is more than 5000 (pagination by fb) 
       friends_data=response.data; 
       dijit.registry.byId("mainWidget_div").set_friends_data(friends_data); 
      } else { 
       alert("Error!"); 
      } 

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

    </script> 

risposta

2

La mia ipotesi è che si sta cercando di ottenere l'elenco di amici prima che l'API di Facebook è completamente inizializzato. Qual è l'errore che stai vedendo?

Si sta registrando la chiamata FB.api da eseguire su DOM ready (dojo.ready). Questo potrebbe causarne il caricamento fuori sincrono, anche se è tutto incluso in fbAsyncInit. La chiamata dell'API amici non ha alcuna dipendenza dal DOM, quindi non lo racchiuderei in una chiamata dojo. Non lo stai facendo nella console e funziona.

Non sono esperto di javascript. Se ho fatto un'ipotesi forse errata, il motivo per cui ciò accade potrebbe avere a che fare con il javascript.

+0

Grazie! Mi hai condotto alla risposta, anche se non è esattamente la stessa di tua ipotesi. – Eva

4

Il answer from Brent Baisley e another answer a una domanda diversa, mi ha aiutato a capire cosa c'era che non andava.

Non è possibile chiamare i metodi dipendenti FB.init() subito dopo FB.init() perché carica in modo asincrono. Anche caricare i dati in modo asincrono come in dojo.ready() non aiuta. È necessario avvolgere il codice in FB.getLoginStatus().

Problemi correlati