使用 Google::Api::V3 将事件添加到另一个用户的日历
Add Event to Calendar of another user using Google::Api::V3
我正在创建一个具有以下 类 的 Web 应用程序。 User
和 Booking
。我需要让更清洁的用户能够批准预订,这又会在房东日历上创建一个活动。
用户有两种类型,cleaners
和hosts
。每个用户都有一个日历。
我发现有两个用例很难满足。
- 房东接受清洁工
Booking
请求;发生这种情况时,Booking
状态更改为已批准,并且活动会添加到主持人日历中。此步骤应该有效,因为 current_user
是 host
,他们已通过 oauth 授权更改其日历。
def new_event(start, endd, cleaner)
client = Signet::OAuth2::Client.new(client_options)
client.update!(session[:authorization])
service = Google::Apis::CalendarV3::CalendarService.new
service.authorization = client
calendar = service.get_calendar(:primary)
today = Date.today
event = Google::Apis::CalendarV3::Event.new({
start: Google::Apis::CalendarV3::EventDateTime.new(date: start),
end: Google::Apis::CalendarV3::EventDateTime.new(date: endd),
summary: "Cleaning Scheduled by #{cleaner}"
})
service.insert_event(calendar.id, event)
end
我在这里调用那个方法
def approve
@booking = Booking.find(params[:id])
@booking.update(status:'approved')
if @booking.save
flash[:success] = "Reservation Approved"
# send_approved_booking_notification(@booking)
new_event(@booking.starts_at.to_date,@booking.starts_at.to_date, @booking.cleaner.name)
redirect_to dashboard_path
else
- 房东要求清洁工并且清洁工批准了预订,当
清洁工批准,应 post 将活动发送给主持人
日历;目前,该事件仅添加到当前
授权用户日历,即本中的清洁工日历
案子。我怎样才能 post 这个事件到用户的日历
创建请求(主机)?
TLDR;
如何使用 Google::Api::CalendarV3 将一个用户日历中的事件添加到另一个用户日历?
我的问题的解决方案是在我的新事件的参数中添加一个 attendee
。在这种情况下,我指定了 host
的电子邮件,并且该事件已添加到清洁工日历和主持人的日历中。这是我的 new_event
方法,它将事件插入到两个日历中
def new_event(start, endd, cleaner, host)
client = Signet::OAuth2::Client.new(client_options)
client.update!(session[:authorization])
service = Google::Apis::CalendarV3::CalendarService.new
service.authorization = client
calendar = service.get_calendar(:primary)
today = Date.today
event = Google::Apis::CalendarV3::Event.new({
start: Google::Apis::CalendarV3::EventDateTime.new(date: start),
end: Google::Apis::CalendarV3::EventDateTime.new(date: endd),
attendees: [{email: host.email}],
summary: "Cleaning Scheduled by #{cleaner}"
})
service.insert_event(calendar.id, event)
end
我正在创建一个具有以下 类 的 Web 应用程序。 User
和 Booking
。我需要让更清洁的用户能够批准预订,这又会在房东日历上创建一个活动。
用户有两种类型,cleaners
和hosts
。每个用户都有一个日历。
我发现有两个用例很难满足。
- 房东接受清洁工
Booking
请求;发生这种情况时,Booking
状态更改为已批准,并且活动会添加到主持人日历中。此步骤应该有效,因为current_user
是host
,他们已通过 oauth 授权更改其日历。
def new_event(start, endd, cleaner)
client = Signet::OAuth2::Client.new(client_options)
client.update!(session[:authorization])
service = Google::Apis::CalendarV3::CalendarService.new
service.authorization = client
calendar = service.get_calendar(:primary)
today = Date.today
event = Google::Apis::CalendarV3::Event.new({
start: Google::Apis::CalendarV3::EventDateTime.new(date: start),
end: Google::Apis::CalendarV3::EventDateTime.new(date: endd),
summary: "Cleaning Scheduled by #{cleaner}"
})
service.insert_event(calendar.id, event)
end
我在这里调用那个方法
def approve
@booking = Booking.find(params[:id])
@booking.update(status:'approved')
if @booking.save
flash[:success] = "Reservation Approved"
# send_approved_booking_notification(@booking)
new_event(@booking.starts_at.to_date,@booking.starts_at.to_date, @booking.cleaner.name)
redirect_to dashboard_path
else
- 房东要求清洁工并且清洁工批准了预订,当 清洁工批准,应 post 将活动发送给主持人 日历;目前,该事件仅添加到当前 授权用户日历,即本中的清洁工日历 案子。我怎样才能 post 这个事件到用户的日历 创建请求(主机)?
TLDR;
如何使用 Google::Api::CalendarV3 将一个用户日历中的事件添加到另一个用户日历?
我的问题的解决方案是在我的新事件的参数中添加一个 attendee
。在这种情况下,我指定了 host
的电子邮件,并且该事件已添加到清洁工日历和主持人的日历中。这是我的 new_event
方法,它将事件插入到两个日历中
def new_event(start, endd, cleaner, host)
client = Signet::OAuth2::Client.new(client_options)
client.update!(session[:authorization])
service = Google::Apis::CalendarV3::CalendarService.new
service.authorization = client
calendar = service.get_calendar(:primary)
today = Date.today
event = Google::Apis::CalendarV3::Event.new({
start: Google::Apis::CalendarV3::EventDateTime.new(date: start),
end: Google::Apis::CalendarV3::EventDateTime.new(date: endd),
attendees: [{email: host.email}],
summary: "Cleaning Scheduled by #{cleaner}"
})
service.insert_event(calendar.id, event)
end