我正在尝试访问包含国定假日的公共日历(来自 Google 日历):
calendarId: 'pt_br.brazilian#holiday@group.v.calendar.google.com'
由于日历是公开的,我想我可以只使用 API 密钥来访问它:
function OnLoadCallback() {
var config = {
client_id: '32j4lk32j5kj342l5h.googleuser.com', //fake client id
scope: 'https://www.googleapis.com/auth/calendar.readonly'
};
gapi.client.setApiKey('fId345AM20HXXXXXXXXXXXXXXXXgT3f9kyp2REfkaw2'); //fake api key
gapi.client.load('calendar', 'v3', function() {
var today = new Date(),
request;
request = gapi.client.calendar.calendarList.get({
calendarId: 'pt_br.brazilian#holiday@group.v.calendar.google.com',
timeMin: (new Date(today.getFullYear(), today.getMonth(), today.getDay(), 0, 0, 0, 0)).toISOString(),
timeMax: (new Date(today.getFullYear(), today.getMonth(), today.getDay(), 23, 59, 59, 999)).toISOString(),
fields: 'items(creator(displayName,email),end,endTimeUnspecified,start,summary)'
});
request.execute(function(response) {
window.alert('length of items: ' + response.items.length);
});
});
}
但是,我不断收到以下响应,这是一个 401(未经授权)错误:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
有人可以澄清我想要做的事情是否可以实现吗?
最后,如果可能的话 - 参考我当前的代码,我必须改变什么?