2

このチュートリアルに従おうとしています

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

JavaScipt を使用して Google カレンダーを実装しますが、次のエラーが発生します。

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

「Authorized Redirect URIs」ボックスの内容をクリアする必要がありました。(JavaScript を使用する場合は、リダイレクトを指定しないでください。)

「Authorized JavaScript Origins」ボックスに、サイトのドメイン名を入力する必要があります。この場合はlocalhost、XAMPP を使用しているためです。

于 2012-10-11T14:49:20.843 に答える