【问题标题】:Google Calendar API javascript Error:Origin MismatchGoogle Calendar API javascript 错误:来源不匹配
【发布时间】:2012-11-06 13:29:26
【问题描述】:

我正在编写我在这里找到的一个简单的 javascript 代码:http://googleappsdeveloper.blogspot.com/2011/12/using-new-js-library-to-unlock-power-of.html

它基本上获得对谷歌日历的身份验证并检索其中包含的事件列表。我已经注册了我的 Web 应用程序并获得了客户端 ID 和 API 密钥。

我正面临这个错误:“错误:来源不匹配”,我正在使用 localhost 在本地运行 javascript,并且我在 google 应用程序注册中也将我的主页设置为 localhost。

任何帮助将不胜感激。

代码:

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

    <script>
    var clientId = '506979856128.apps.googleusercontent.com';
var apiKey = 'AIzaSyAGbQAZQU0YNL8hK5EU69exIg7_sOg3JoA';
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);
      }
    });
 });
}
    </script>
    <script src="https://apis.google.com/js/client.js?onload=handleClientLoad">                 </script>
</body>
</html>

【问题讨论】:

    标签: javascript oauth google-calendar-api


    【解决方案1】:

    我今天得到了同样的错误来源不匹配。经过一番搜索,我得到了原因。

    在创建 Google Api 访问时,我们必须指定 授权重定向 URIs授权重定向 URIs

    现在,如果您从 授权重定向 URIs 中指定的 URI 调用登录,error:Unknown Origin is thrown

    仅供参考:我看到您使用 localhost 在本地运行 javascript。 这意味着您尚未将 localhost uri 添加到授权重定向 URI。

    但是不要浪费你的时间。授权重定向 URI 不会接受 localhost uri。这是由于 Chrome 的同源策略。如果你使用 disable-web-security 标志运行 chrome,它也可以在本地工作.

    【讨论】:

    • 听起来很合理但是... cd "/Applications/Google Chrome Canary.app/Contents/MacOS/" --> ./Google\ Chrome\ Canary --disable-web-security --> Trace/BPT trap: 5 --> Google Chrome quit unexpectedly.
    • @CraigMorgan:FF 中的 chrome 扩展??
    • 不——只是普通的老FF
    【解决方案2】:

    Google 中创建 Client ID 时注意 Redirect URIsJavascript Origins 可以解决来源不匹配的问题开发者控制台

    Javascript 起源应该/结尾。

    示例http://example.com/ --> 正确的格式是http://example.com

    【讨论】:

      猜你喜欢
      • 2017-03-10
      • 2022-06-22
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多