小工具OAUTH请求返回内部服务器错误

时间:2014-11-10 10:54:42

标签: oauth google-calendar-api google-gadget opensocial

我正在开发一个小工具(html, JS)以在Google Calendar page内运行。我的OAUTH授权 工作正常但只有1小时。如果我每5分钟重复一次相同的请求,我会在1小时内获得良好的结果,然后我会永远得到"[500]Internal server error"。原因是令牌不好。我在1小时后得到一个新的(我可以在console.log(shindig.auth.getSecurityToken())Network tab (makeRequest details, Form data, st)上看到它),但它会导致"[500]Internal Server Error"。 2小时后情况保持不变:我获得新令牌,但会导致"[500]Internal Server Error"。我做错了什么?

<?xml version="1.0" encoding="UTF-8" ?> 
<Module>
  <ModulePrefs title="test_gadget">
    <Require feature="opensocial-0.8" />
    <Require feature="locked-domain"/>
    <Require feature='auth-refresh'/>
      <OAuth>
      <Service name="google">
        <Access url="https://www.google.com/accounts/OAuthGetAccessToken" method="GET" /> 
        <Request url="https://www.google.com/accounts/OAuthGetRequestToken?scope=https://www.googleapis.com/auth/tasks" method="GET" />
        <Authorization url="https://www.google.com/accounts/OAuthAuthorizeToken?oauth_callback=http://oauth.gmodules.com/gadgets/oauthcallback" /> 
      </Service>
    </OAuth>
  </ModulePrefs>
  <Content type="html">
  <![CDATA[
      <!-- shindig oauth popup handling code -->
      <!-- <script src="http://gadget-doc-examples.googlecode.com/svn/trunk/opensocial-gadgets/popup.js"></script> -->
      <script type="text/javascript" src="https://rawgit.com/Appiens/daybyday_gadget/master/javascript/shindig.js"></script>
      <script type="text/javascript">

      var requestInterval = 5 * 60 * 1000; 
      var timerFetch = -1; 
      var API_KEY = 'AIzaSyCuKllVMlv0ENk8Skg8_-IKM1Cs9GeL-NU';

       function fetchData() {
           var params = {};
           url = "https://www.googleapis.com/tasks/v1/users/@me/lists?key=" + API_KEY;
           params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
           params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.OAUTH;
           params[gadgets.io.RequestParameters.OAUTH_SERVICE_NAME] = "google";
           params[gadgets.io.RequestParameters.OAUTH_USE_TOKEN] = "always";
           params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
           gadgets.io.makeRequest(url, OnFetchData, params);
       }

       function OnFetchData(response) {
            var d = new Date();
            var token = shindig.auth.getSecurityToken();

        if (response.oauthApprovalUrl) {

              var popup = shindig.oauth.popup({
                    destination: response.oauthApprovalUrl,
                    windowOptions: null,
                    onOpen: function() { showOneSection('waiting'); },
                    onClose: function() { fetchData(); }
              });

              var personalize = document.getElementById('personalize');
              personalize.onclick = popup.createOpenerOnClick();
              var approvaldone = document.getElementById('approvaldone');
              approvaldone.onclick = popup.createApprovedOnClick();
              showOneSection('approval');
            } 
        else if (response.data) {
         console.log(d + ' ' + token);
                  var taskLists = [];
                  taskLists = response.data.items;
                  for(var i=0; i< taskLists.length; i++) {
                    console.log(taskLists[i].title);
                  }

                  if (document.getElementById('main').style.display = 'none') {
                        showOneSection('main');
                  }

                }
               else {
                 console.log(d + ' ' + token);
                 console.log( JSON.stringify(response));

                 if (document.getElementById('main').style.display = 'none') {
                        showOneSection('main');
                  }
               }

           timerFetch = setTimeout(fetchData , requestInterval);
      }

      function showOneSection(toshow) {
        var sections = [ 'main', 'approval', 'waiting'];
        for (var i=0; i < sections.length; ++i) {
            var s = sections[i];
            var el = document.getElementById(s);
            if (s === toshow) {
                el.style.display = "block";
            } else {
                el.style.display = "none";
            }
        }
      }

       gadgets.util.registerOnLoadHandler(fetchData);
      </script>   

      <div id="main" style="display: none;">
      </div>

      <div id="approval" style="display: none">
        <a href="#" id="personalize">Personalize this gadget</a>
      </div>
      <div id="waiting" style="display: none">
        Please click
      <a href="#" id="approvaldone">I've approved access</a>
        once you've approved access to your data.
      </div>
    ]]> 
  </Content>
</Module>

0 个答案:

没有答案