2

我正在尝试遵循本教程

(http://googleappsdeveloper.blogspot.co.uk/2011/12/using-new-js-library-to-unlock-power-of.html)

使用 JavaScipt 实现 Google Calender 但我收到以下错误:

Error: origin_mismatch

Request Details
scope=https://www.googleapis.com/auth/calendar
response_type=token
access_type=online
redirect_uri=postmessage
proxy=oauth2relay874729177
origin=http://localhost
state=538915583
display=page
client_id=553681888475.apps.googleusercontent.com
authuser=0

我的 JS 文件:

var clientId = '553681888475.apps.googleusercontent.com';
var apiKey = 'AIzaSyCh86BTa79UfqU3I_Y0jiWv2g3eXvMh6XY';
var scopes = 'https://www.googleapis.com/auth/calendar';

function handleClientLoad() {
  gapi.client.setApiKey(apiKey);
  window.setTimeout(checkAuth,1);
  checkAuth();
}

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 makeApiCall() {
  gapi.client.load('calendar', 'v3', function() {
    var request = gapi.client.calendar.events.list({
      'calendarId': 'primary'
    });

    request.execute(function(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);
      }
    });
  });
}

HTML:

<html>
  <body>
    <div id='content'>
      <h1>Events</h1>
      <ul id='events'></ul>
    </div>
    <a href='#' id='authorize-button' onclick='handleAuthClick();'>Login</a>

    <script src="../Controller/Cal.js">
    </script>
    <script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
  </body>
</html>
4

1 回答 1

2

我必须清除“授权重定向 URI”框中的内容。(使用 JavaScript 时,不要指定任何重定向。)

在“Authorized JavaScript Origins”框中,您必须输入您网站的域名,在这种情况下,就像localhost我使用 XAMPP 一样。

于 2012-10-11T14:49:20.843 回答