通过JavaScript未经授权访问Google Calendar API [英] Accessing Google Calendar API without authorization via JavaScript
问题描述
我尝试访问包含全国假日的公共日历(来自Google日历):
calendarId:'pt_br .brazilian#holiday @ group.v.calendar.google.com'
由于日历是公开的,我认为我只能使用API Key访问它:
函数OnLoadCallback(){
var config = {
client_id:'32j4lk32j5kj342l5h.googleuser.com',//假客户ID
范围:'https://www.googleapis.com/auth/calendar.readonly'
};
gapi.client.setApiKey('fId345AM20HXXXXXXXXXXXXXXXXgT3f9kyp2REfkaw2'); //假api键
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('items of length:'+ response.items.length);
} );
});
}
然而,我一直得到以下回应,这是401(未授权)错误:
{
错误:{
错误:[
{
domain:global,
reason:required,
message:需要登录,
locationType:header,
location:Authorization
}
],
code:401,
message:需要登录
}
有人可以澄清我试图做的事是否可以实现吗? >
最后如果可能的话 - 我必须改变什么来引用当前的代码?
我能够用jQuery做同样的事情。
var mykey ='your_api_key'; //通常类似于Gtg-rtZdsreUr_fLfhgPfgff
var calendarid ='you_calendar_id'; //看起来有点像3ruy234vodf6hf4sdf5sd84f@group.calendar.google.com
$ b $ .ajax({
类型:'GET',
url:encodeURI('https:/ /www.googleapis.com/calendar/v3/calendars/'+ calendarid +'/ events?key ='+ mykey),
dataType:'json',
success:function(response){
//对每个
做任何你想做的事
error:function(response){
//告诉发生错误
}
}) ;
然而,您需要确保您已完成以下操作: - $ / b
1)在 https ://code.google.com/apis/console
2)生成一个简单的API访问密钥
3)确保Calendar API在服务下激活。 p>
通过 https://开发人员了解详情.google.com / google-apps / calendar / firstapp
I'm trying to access a public calendar (from Google Calendar) that contains national holidays:
calendarId: 'pt_br.brazilian#holiday@group.v.calendar.google.com'
As the calendar is public, I thought I would be able to access it using only the API Key:
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);
});
});
}
However, I keep getting the following response, which is a 401 (unauthorized) error:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
Can someone clarify whether what I'm trying to do is achievable or not?
And finally if it is possible - what do I have to change referring to my current code?
I was able to do the same thing using jQuery.
var mykey = 'your_api_key'; // typically like Gtg-rtZdsreUr_fLfhgPfgff
var calendarid = 'you_calendar_id'; // will look somewhat like 3ruy234vodf6hf4sdf5sd84f@group.calendar.google.com
$.ajax({
type: 'GET',
url: encodeURI('https://www.googleapis.com/calendar/v3/calendars/' + calendarid+ '/events?key=' + mykey),
dataType: 'json',
success: function (response) {
//do whatever you want with each
},
error: function (response) {
//tell that an error has occurred
}
});
However you need to be sure you have done the following:-
1) Register a project at https://code.google.com/apis/console
2) Generate a Simple API Access key
3) Ensure Calendar API is activated under services.
Read more at https://developers.google.com/google-apps/calendar/firstapp
这篇关于通过JavaScript未经授权访问Google Calendar API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!