2012-08-11 15 views
7

¿Es posible acceder (v3 calendario) de la API de Google desde un archivo HTML local (con javascript)? Quiero abrir c: \ temp \ gsotto \ gsotto.htm en mi navegador en lugar de servir el archivo a través de IIS.acceso a Google API s local HTML/Javascript (sin servidor web)

funciona el servicio al archivo desde mi

http://localhost/ 

a través de un servidor web. En la consola de API de Google Tengo un "ID de cliente para aplicaciones web" con:

Redirect URIs: http://localhost 
JavaScript origins:  http://localhost 

y una clave de "acceso a la API simple" para las aplicaciones del navegador (con referers)

Firebug shows me this when accessing through http://localhost/gsotto/gsotto.htm 
GET http://localhost/gsotto/gsotto.htm 
GET https://apis.google.com/js/client.js?onload=handleClientLoad 
GET https://apis.google.com/_/apps-static/_/js/gapi/client....cb=gapi.loaded_0 
GET https://ssl.gstatic.com/accounts/o/...-postmessagerelay.js 
GET https://accounts.google.com/o/oauth2/auth?client_id=.....&authuser=0 
GET https://ssl.gstatic.com/accounts/o/....-postmessage.js 

and this when access through c:\... 
GET https://apis.google.com/js/client.js?onload=handleClientLoad 
GET https://apis.google.com/_/apps-static/_/js/gapi/client.....cb=gapi.loaded_0 
GET https://ssl.gstatic.com/accounts/o/.....-postmessagerelay.js 
and nothing more.... 

do i need to use other settings in the google api console for this to work? 


<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset='utf-8' /> 
    </head> 
    <body> 
    <!--Add a button for the user to click to initiate auth sequence --> 
    <button id="authorize-button" style="visibility: hidden">Authorize</button> 
    <script type="text/javascript"> 
     // google calendar id 
     var calId = "...."; 

     var clientId = "..."; // oAuth2 webapp 
     var apiKey = "....";// Key for browser apps (with referers) 

     // google authentication scopes 
     var scopes = 'https://www.googleapis.com/auth/calendar'; 
       //https://www.googleapis.com/auth/calendar.readonly 

     // Use a button to handle authentication the first time. 
     function handleClientLoad() { 
      console.log('handleClientLoad'); 
     gapi.client.setApiKey(apiKey); 
     window.setTimeout(checkAuth,1); 
     } 

     function checkAuth() { 
      console.log('checkAuth'); 
      try { 

     gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult); 
      } 
      catch(e) 
      { 
       console.log('e'); 
       console.log(e); 
      } 
     } 


     function handleAuthResult(authResult) { 

      console.log('handleAuthResult'); 
     var authorizeButton = document.getElementById('authorize-button'); 
     if (authResult && !authResult.error) { 
      console.log('result ok'); 
      authorizeButton.style.visibility = 'hidden'; 
      makeApiCall(); 
     } else { 
      console.log('authresult null or error'); 
      console.log(authResult); 
      authorizeButton.style.visibility = ''; 
      authorizeButton.onclick = handleAuthClick; 
     } 
     } 

     function handleAuthClick(event) { 
     console.log('handleAuthClick'); 
     gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult); 
     return false; 
     } 

function makeApiCall() { 
     console.log('makeApiCall'); 
    gapi.client.load('calendar', 'v3', function() { 
    var request = gapi.client.calendar.events.list({ 
     'calendarId': calId 
    }); 

    request.execute(function(resp) { 
     console.log('result:'); 
     console.log(resp); 

     for (var i = 0; i < resp.items.length; i++) { 
     var li = document.createElement('li'); 
     li.appendChild(document.createTextNode(resp.items[i].summary)); 
     document.getElementById('events').appendChild(li); 
     } 
    }); 
    }); 
} 
    </script> 
    <script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script> 
    <div id="content"> 
    <ul id="events"></ul> 
    </div> 
    </body> 
</html> 
+0

* "¿Es posible?" * - qué incluso intentar antes de pedir? – Joseph

+0

Es posible, puede ser necesario para permitir la escritura ejecuta en IE –

+0

Yo probé, pero no obtuvo resultados. Se editó la pregunta para incluir el resultado NET-log de source y firebug – sotto

Respuesta

5

Dependiendo de su navegador se encuentra que hay AJAX es posible cuando se ejecuta desde el protocolo file:// por razones de sitios cruzados (cross-o protocolo). Los GET que ve trabajando no son XHR/AJAX, sino <script>, por lo que la respuesta es No para la mayoría de los navegadores modernos. Vea el discussion here. Es posible que funcione si ejecuta Chrome con --allow-file-access-from-files y --disable-web-security (link).