我工作的一個簡單的JavaScript代碼,我發現這裏:http://googleappsdeveloper.blogspot.com/2011/12/using-new-js-library-to-unlock-power-of.html谷歌日曆API JavaScript錯誤:產地不匹配
它基本上獲得認證的谷歌日曆和檢索包含的事件列表。我已經註冊了我的Web應用程序並獲得了客戶端ID和API密鑰。
我面對這個錯誤:「錯誤:來源不匹配」,我在本地使用本地主機運行JavaScript,並將我的主頁也設置在Google應用程序註冊到本地主機。
任何幫助將非常感激。
代碼:
<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>
聽起來很合理,但... '''cd「/ Applications/Google Chrome Canary.app/Contents/MacOS/」''' - >'''./Google \ Chrome \ Canary --disable- web-security''' - >'''Trace/BPT陷阱:5''' - >'''Google Chrome意外退出.''' –
在Firefox中運行...正常工作 –
@CraigMorgan:chrome FF中的擴展? –