所以我有这个Ruby on rails应用程序,我有一个服务帐户设置为谷歌日历API服务器到服务器的请求。我已经得到了日历对象,它的方法包括insert_event.
class CalendarController < ApplicationController
require 'googleauth'
require 'google/apis/calendar_v3'
def create_event
calendar = Google::Apis::CalendarV3::CalendarService.new
scopes = ['https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/drive']
calendar.authorization = Google::Auth.get_application_default(scopes)
token = calendar.authorization.fetch_access_token!
event = {
'summary' => 'Google I/O 2015',
'location' => '800 Howard St., San Francisco, CA 94103',
'description' => 'A chance to hear more about Google's developer products.',
'start' => {
'dateTime' => '2015-05-28T09:00:00-07:00',
'timeZone' => 'America/Los_Angeles',
},
'end' => {
'dateTime' => '2015-05-28T17:00:00-07:00',
'timeZone' => 'America/Los_Angeles',
},
'recurrence' => [
'RRULE:FREQ=DAILY;COUNT=2'
],
'attendees' => [
{'email' => 'myemail1@gmail.com'},
{'email' => 'jdong8@gmail.com'},
],
'reminders' => {
'useDefault' => false,
'overrides' => [
{'method' => 'email', 'minutes' => 24 * 60},
{'method' => 'popup', 'minutes' => 10},
],
},
}
calendar.insert_event(event, 'primary')
end
end
当我尝试运行日历。insert_event(event, 'primary')我得到这个404错误
404 (165 bytes) 338ms>
{"domain"=>"global", "reason"=>"notFound", "message"=>"Not Found"}
Caught error {"domain"=>"global", "reason"=>"notFound", "message"=>"Not Found"}
Error - #<Google::Apis::ClientError: {"domain"=>"global", "reason"=>"notFound", "message"=>"Not Found"}>
Google::Apis::ClientError: {"domain"=>"global", "reason"=>"notFound", "message"=>"Not Found"}
google日历API的主要文档围绕客户端对象使用了不同的设置,该设置与建议创建日历对象的服务帐户文档不匹配。有人知道如何做到这一点,即使它是一个非常不同的实现理想情况下,虽然我想知道什么?我希望当客户提出交付请求时,我能够在我的日历上添加内容。
问题主要出在事件上,对于这个设置,它必须是一个特殊的google对象,而不是一个散列。下面是让它工作的代码,我在gems/google-api-client-0.9.pre1/samples/calendar/calendar.rb
中找到了它require 'googleauth'
require 'google/apis/calendar_v3'
calendar = Google::Apis::CalendarV3::CalendarService.new
scopes = ['https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/drive']
calendar.authorization = Google::Auth.get_application_default(scopes)
token = calendar.authorization.fetch_access_token!
emails = ["me@example.com","myboss@example.com"]
# Create an event, adding any emails listed in the command line as attendees
event = Calendar::Event.new(summary: 'A sample event',
location: '1600 Amphitheatre Parkway, Mountain View, CA 94045',
attendees: emails.each { |email| Calendar::EventAttendee.new(email: email) },
start: Calendar::EventDateTime.new(date_time: DateTime.parse('2015-12-31T20:00:00')),
end: Calendar::EventDateTime.new(date_time: DateTime.parse('2016-01-01T02:00:00')))
event = calendar.insert_event('primary', event, send_notifications: true)
puts "Created event '#{event.summary}' (#{event.id})"
end