0

所以我让这个 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

当我尝试运行 calendar.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"}

谷歌日历 API 的主要文档围绕客户端对象使用不同的设置,该设置与建议制作日历对象的服务帐户文档不匹配。有没有人知道如何做到这一点,即使它是一个非常不同的实现理想情况下我想知道是什么?每当客户提出交货请求时,我希望能够将东西放在我的日历上。

4

1 回答 1

3

原来问题主要出在事件上,对于这个设置,它必须是一个特殊的 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
于 2015-07-17T03:25:00.020 回答