我有一个简单的代码,可以登录Google使用Google Calendar API并为用户创建和发送事件。代码工作正常,但每次我都会收到来自Google的401 - Login Required错误。如果我在浏览器中打开开发人员控制台并再试一次它......这很奇怪
var scopes = 'https://www.googleapis.com/auth/calendar';
var clientId = 'CLIENT_ID';
var apiKey = 'API_KEY';
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) {
var authorizeButton = $('#calendarAuth');
var calendarFrame = $('#calendarFrame');
if (authResult && !authResult.error) {
authorizeButton.remove();
calendarFrame.show();
calendarFrame.attr('src', calendarFrame.attr('src')); //Hack to reload the iframe
} else {
authorizeButton.show();
calendarFrame.hide();
}
}
function Authorize() {
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
return false;
}
function GoogleScheduleFollowup(followup) {
gapi.client.load('calendar', 'v3', function () {
var request = gapi.client.calendar.events.insert({
"calendarId": "primary",
resource: {
"summary": followup.title,
"start": {
"dateTime": window.GetFullDateString(followup.date, followup.time)
},
"end": {
"dateTime": window.GetFullDateString(followup.date, followup.time)
}
}
});
request.execute(function (resp) {
console.log(resp);
});
}
在API控制台中似乎已经完成了所有配置,我确信我们还没有达到配额(总计或每秒请求数)。作为一个例子,我已经提出了133个请求,其中36个因此错误而失败。
我曾尝试每隔10分钟调用一次gapi.auth.authorize来查看问题是否是会话超时,而当我在另一个问题中读到时,我尝试删除此行gapi.client.setApiKey(apiKey);
,两者均未成功