使用Google Calendar API Rails时没有方法错误

时间:2018-06-25 18:46:48

标签: ruby-on-rails ruby google-api google-calendar-api

当尝试使用Google Calendar API文档中引用的 insert_event 方法时,我的rails应用程序返回未定义的方法'insert_event'

我已经安装了google api客户端gem,是否缺少可以让我使用此方法的内容?我一直在看几个指南和视频,即使我确定我缺少一些简单的东西,它似乎也可以自动运行

我的代码本身:

def new_event
client = GoogleCalendar.new(request: request)

event = Google::Apis::CalendarV3::Event.new({
 start: {
  date_time: Time.now,
  time_zone: 'America/Chicago',
  },
  end: {
  date_time: Time.now + 1,
  time_zone: 'America/Chicago'
 },
  summary: 'New event!'
  })


result = client.insert_event('primary', event)
end

1 个答案:

答案 0 :(得分:0)

我将Google身份验证与“ Signet gem”(而不是GoogleCalendar)一起使用。它是谷歌的官方红宝石。它支持OAuth 1.0 / OAuth 2.0实施。

def new_event
calendars

client = Signet::OAuth2::Client.new(client_options)
client.update!(session[:authorization])

service = Google::Apis::CalendarV3::CalendarService.new
service.authorization = client

today = Date.today

event = Google::Apis::CalendarV3::Event.new({
  start: Google::Apis::CalendarV3::EventDateTime.new(date: today),
  end: Google::Apis::CalendarV3::EventDateTime.new(date: today + 1),
  summary: params[:summary],
  description: params[:description],
  send_notifications: true
})

#binding.pry       #service.insert_event(params [:calendar_id],事件)

 service.insert_event("primary", event)

 redirect_to events_url(calendar_id: params[:calendar_id])

结束