通过 JavaScript 未经授权访问 Google Calendar API [英] Accessing Google Calendar API without authorization via JavaScript

查看:31
本文介绍了通过 JavaScript 未经授权访问 Google Calendar API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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'

由于日历是公开的,我认为我可以仅使用 API 密钥访问它:

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);
        });

    });
}

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

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?

推荐答案

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

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) 在 https://code.google.com/apis/console
2) 生成一个简单的 API 访问密钥
3) 确保在服务下激活日历 API.

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.

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

这篇关于通过 JavaScript 未经授权访问 Google Calendar API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆