通过 JavaScript 未经授权访问 Google Calendar API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13651017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:35:19  来源:igfitidea点击:

Accessing Google Calendar API without authorization via JavaScript

javascriptgoogle-apioauth-2.0google-calendar-api

提问by fegemo

I'm trying to access a public calendar (from Google Calendar) that contains national holidays:

我正在尝试访问包含国定假日的公共日历(来自 Google 日历):

calendarId: 'pt_br.brazilian#[email protected]'

As the calendar is public, I thought I would be able to access it using only the API Key:

由于日历是公开的,我认为我只能使用 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#[email protected]',
            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:

但是,我不断收到以下响应,这是 401(未经授权)错误:

{
 "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?

有人可以澄清我正在尝试做的事情是否可以实现吗?
最后,如果可能的话 - 参考我当前的代码我必须改变什么?

回答by Sola Oderinde

I was able to do the same thing using jQuery.

我能够使用 jQuery 做同样的事情。

var mykey = 'your_api_key'; // typically like Gtg-rtZdsreUr_fLfhgPfgff
var calendarid = 'you_calendar_id'; // will look somewhat like [email protected]

$.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.

1) 在https://code.google.com/apis/console注册一个项目
2) 生成一个简单的 API 访问密钥
3) 确保在服务下激活日历 API。

Read more at https://developers.google.com/google-apps/calendar/firstapp

https://developers.google.com/google-apps/calendar/firstapp阅读更多信息