使用Google API访问公共日历,而无需用户登录 [英] Accessing a public calendar using Google API without requiring a user to log in
问题描述
我想使用Google的REST API访问公共日历.
Google的日历API建议我需要OAuth令牌才能访问日历:
https://developers.google.com/google-apps/calendar/auth >
但是,我正在访问公共日历,并且正在服务器上进行此操作,因此可以(不必)要求用户进行身份验证.
我正在使用node.js api:
googleapis
.discover('calendar', 'v3').execute(function(err, client) {
client.calendar.calendars.get({ calendarId: '***@group.calendar.google.com' })
.execute(function (err, response) {
console.log('response from google', response);
});
});
这将返回您的客户发出了格式错误或非法的请求.仅此而已."
在.calendars.get()
之后调用.withApiKey('***')
会返回相同的错误.
有什么建议吗?
文档多次声明
对Google Calendar API的所有请求都必须由经过身份验证的用户授权.
因此,如果您想使用Google Calendar API,则需要经过身份验证的用户.使用 Passport.js 和 http://imgur.com/UGjPtqp
您可以从以下位置公开访问数据:
- xml https ://www.google.com/calendar/feeds/en.uk%23holiday%40group.v.calendar.google.com/public/basic
- ical https://www.google.com/calendar/ical/en.uk%23holiday%40group.v.calendar.google.com/public/basic.ics
- html https://developers.google.com/google-apps/calendar/auth
However, I'm accessing a public calendar and am doing this from the server so can't/shouldn't have to ask the user to authenticate.
I'm using the node.js api:
googleapis .discover('calendar', 'v3').execute(function(err, client) { client.calendar.calendars.get({ calendarId: '***@group.calendar.google.com' }) .execute(function (err, response) { console.log('response from google', response); }); });
This returns "Your client has issued a malformed or illegal request. That’s all we know."
Calling
.withApiKey('***')
after.calendars.get()
returns the same error.Any suggestions?
解决方案As the documentation states more than once
All requests to the Google Calendar API must be authorized by an authenticated user.
Hence, if you want to use the google calendar API you need an authenticated user. This can be easily achieved using modules like Passport.js and Passport-google-oauth.
However, if you can leave aside the API for a moment and
- the calendar is public
- you want read only access
you can easily grab the public address and consume the calendar through ical, xml or html. Have a look at the UK holidays public calendar: http://imgur.com/UGjPtqp
You can access the data publicly from:
- xml https://www.google.com/calendar/feeds/en.uk%23holiday%40group.v.calendar.google.com/public/basic
- ical https://www.google.com/calendar/ical/en.uk%23holiday%40group.v.calendar.google.com/public/basic.ics
- html https://www.google.com/calendar/embed?src=en.uk%23holiday%40group.v.calendar.google.com&ctz=Europe/London
At this point, it's trivial to fetch such resources with node and get the data you want.
这篇关于使用Google API访问公共日历,而无需用户登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!