2

尝试允许用户使用带有 javascript 的 Google 日历 API v3 进行身份验证和快速添加事件。我没有看到身份验证按钮。我对编码非常缺乏经验。

安慰:

Uncaught SyntaxError: Unexpected token } test.html:47
Uncaught TypeError: Cannot read property 'qainput' of undefined test.html:62
onload test.html:62

.html 文件:

<html>
  <head>
    <meta charset='utf-8' />
    <style>
      #info {
        border: 0px solid black;
        padding: 0.25em;
        margin: 0.5em 0;
      }
    </style>
    <script type="text/javascript"> 
      var apiKey = 'AIzaSyDcbjOvAT85hCdVrjgUAqylf_QtxE2Gx60';
      var clientId = '202852486259.apps.googleusercontent.com';
      var scopes = 'https://www.googleapis.com/auth/calendar';
      function handleClientLoad() {
        gapi.client.setApiKey(apiKey);
        window.setTimeout(checkAuth,1);
      }

      function checkAuth() {
        gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
      }


      function handleAuthResult(authResult) {
        var authorizeButton = document.getElementById('authorize-button');
        if (authResult) {
          authorizeButton.style.visibility = 'hidden';
          makeApiCall();
        } else {
          authorizeButton.style.visibility = '';
          authorizeButton.onclick = handleAuthClick;
        }
      }

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

      function makeRpcRequest() {
        var qatext = document.qaform.qainput.value;
        var request = gapi.client.calendar.events.quick_add({
          'calendarId': 'primary',
          'text': +qatext+
        });
        request.execute(writeResponse);
      }

      function writeResponse(response) {
        console.log(response);
        var name = response.summary;
        var infoDiv = document.getElementById('info');
        var infoMsg = document.createElement('P');
        infoMsg.appendChild(document.createTextNode(+ name' sucessfully created!'));
        infoDiv.appendChild(infoMsg);
      }
    </script>
    <script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
  </head>
  <body onload="document.qaform.qainput.focus();">
    <button id="authorize-button" style="visibility: hidden">Authorize to Use QuickAdd</button>
    <form id="qaform">
    <input placeholder='QuickAdd' name='qainput' />
    <button id="rpc" onclick="makeRpcRequest();">Add</button>
    </form>
    <div id="info"></div>
  </body>
</html>
4

1 回答 1

1

几个星期以来,您可能已经找到了答案,但我最近遇到了同样的问题,从您使用的代码基本相同。我根本没有在谷歌控制台的“服务”下激活谷歌日历。如果所有控制台设置不正确,“授权”按钮将保持隐藏状态(默认隐藏)。一旦所有设置正确并且脚本与 api 正确通信,授权按钮就会变得可见,然后您可以添加事件并通过单击按钮编写响应。

于 2012-06-05T01:50:40.273 回答