以下代码在使用Chrome或Firefox时效果很好。以下是Chrome / Firefox的处理方式。
但是,它不适用于IE。以下是IE的处理方式。
有没有人遇到过类似的问题?或者有任何解决方法吗?我一直在寻找解决方案,但仍然找不到任何解决方案。似乎也有人面临类似的问题。
以下代码取自Google APIs Client Library for Javascript.
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<script src='js/jquery-1.11.1.min.js'></script>
</head>
<body>
<button id="authorize-button" style="visibility: hidden">Authorize</button>
<script type="text/javascript">
var clientId = '{CLIENT ID}';
var apiKey = '{API KEY}';
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) {
console.log(authResult);
var authorizeButton = document.getElementById('authorize-button');
if (authResult && !authResult.error) {
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.calendarList.list();
request.execute(function(resp){
$.each( resp.items, function( key, value ) {
console.log(resp.items[key].id);
});
});
});
}
</script>
<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
<div id="content"></div>
<p>Retrieves Calendar ID using the Google Plus API.</p>
</body>
</html>
更新的
试图找出可能出错的地方,并发现尽管代码相同,“authResult”会返回不同的结果。
Chrome / Firefox结果:
[object Object]
{
_aa: "1",
access_token: "{ACCESS TOKEN}",
client_id: "{CLIENT ID}",
cookie_policy: undefined,
expires_at: "1414246430",
expires_in: "3600",
g_user_cookie_policy: undefined,
issued_at: "1414160131",
response_type: "token",
scope: "https://www.googleapis.com/auth/calendar",
state: [object Object],
token_type: "Bearer"
}
IE结果:
[object Object]
{
client_id: "{CLIENT ID}",
cookie_policy: undefined,
error: "immediate_failed",
error_subtype: "access_denied",
expires_at: "1414246430",
expires_in: "86400",
g_user_cookie_policy: undefined,
issued_at: "1414160030",
response_type: "token",
scope: "https://www.googleapis.com/auth/calendar",
state: "",
status: { }
}