Google日历添加活动"插入"方法不起作用

时间:2017-06-22 14:56:17

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

这是我第一次使用Google API,而且在我自己的Google日历中添加来自我的本地Rails应用程序的存根事件时,我发现了一个完全障碍。

我已尝试按照this演练,但它有很多我不需要的动作。我只希望能够创建一个活动,并能够在Google日历上在线查看。

当尝试使用与该教程完全相同的代码发布时,我在最后一个位置因未知原因而收到与missing keys [calendar_id]相关的错误:

service.insert_event(params[:calendar_id], event)

因此,在放弃完成这项工作之后,我尝试在另一个找到here的解决方案之后进行实际的POST操作,特别是最后一部分,

@set_event = client.execute(:api_method => service.events.insert,
                    :parameters => {'calendarId' => current_user.email, 'sendNotifications' => true},
                    :body => JSON.dump(@event),
                    :headers => {'Content-Type' => 'application/json'})

但是,Rails告诉我events中的service.events.insert是未定义的方法。

我非常沮丧,因为我花了大部分时间试图修复它。 Oath重定向和身份验证有效,但我无法创建事件。这是我的代码:

###Controller
class Api::V2::CalendarsController < Api::V2::BaseController
 require 'signet/oauth_2/client'
 require 'google/apis/calendar_v3'
 require 'googleauth'

def redirect
 client = Signet::OAuth2::Client.new({
  client_id: Figaro.env.google_client_id,
  client_secret: Figaro.env.google_client_secret,
  authorization_uri: 'https://accounts.google.com/o/oauth2/auth',
  scope: Google::Apis::CalendarV3::AUTH_CALENDAR,
  redirect_uri: 'http://localhost:3000/'
 })
 redirect_to client.authorization_uri.to_s
end

def callback
 client = Signet::OAuth2::Client.new({
  client_id: Figaro.env.google_client_id,
  client_secret: Figaro.env.google_client_secret,
  token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
  redirect_uri: callback_url,
  code: params[:code]
})

response = client.fetch_access_token!

session[:authorization] = response

redirect_to 'http://localhost:3000'
end

def new_event

client = Signet::OAuth2::Client.new({
  client_id: Figaro.env.google_client_id,
  client_secret: Figaro.env.google_client_secret,
  token_credential_uri: 'https://accounts.google.com/o/oauth2/token'
})

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: 'New event!'
})

begin
  event = service.insert_event('primary', event)
 rescue Google::Apis::AuthorizationError => exception
  response = client.refresh!

  session[:authorization] = session[:authorization].merge(response)

  retry
 end
end

###Route
namespace :api, defaults: { format: :json } do
 namespace :v2 do
  get '/redirect', to: 'calendars#redirect', as: 'redirect'
  get '/callback', to: 'calendars#callback', as: 'callback'
  post '/events/new', to: 'calendars#new_event', as: 'new_event'
 end
end

###View snippet
<%= form_tag api_v2_new_event_path do %>
  <%= submit_tag 'Add event' %>
<% end %>

如果有人能指出我这两种变体的实际错误,或者是否有更好的方法来实际创建和提交活动,我真的很感激。谢谢!

更新:按照@ SergeyBokolov的建议,我更新了代码。但是,我现在在尝试提交时得到这个:

Authorization failed.  Server message:
{
 "error" : "invalid_request",
 "error_description" : "Required parameter is missing: grant_type"
}

1 个答案:

答案 0 :(得分:0)

service.insert_event('primary', event)

您有missing keys [calendar_id],因为params[:calendar_id]为空。

第二个链接的教程看起来过时了。

可插入事件的官方示例here

更新: 您的新错误与授权有关。我建议您使用下一个代码进行授权:

service.authorization = Google::APIClient::ClientSecrets.new(
        {'web' => {'access_token' => YOUR_TOKEN,
                   'refresh_token' => YOUR_REFRESH_TOKEN,
                   'client_id' => YOUR_GOOGLE_APP_ID,
                   'client_secret' => YOUR_GOOGLE_APP_SECRET}}
    ).to_authorization