Google 日历 Ruby API insert_event 错误 "each_with_index" 未定义

Google Calendar Ruby API insert_event errors "each_with_index" undefined

我正在复制并粘贴 Ruby Quickstart and combining it with the create-events code sample from here 中的代码。

我唯一改变的是:

尽管如此,我还是收到了错误:

.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/representable-3.0.4/lib/representable/pipeline.rb:38:in `call': undefined method `each_with_index' for nil:NilClass (NoMethodError)

这令人困惑,none 比之前知道它能正常工作更令人困惑。我没有捕捉到的例子中有明显的错误吗?还是 representable 或 api-client 突然逃跑了?

人们是否能够重现该问题?这是我的代码以及我的 Gemfile.lock

require "google/apis/calendar_v3"
require "googleauth"
require "googleauth/stores/file_token_store"
require "date"
require "fileutils"

OOB_URI = "urn:ietf:wg:oauth:2.0:oob".freeze
APPLICATION_NAME = "Google Calendar API Ruby Quickstart".freeze
CREDENTIALS_PATH = "credentials.json".freeze
# The file token.yaml stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
TOKEN_PATH = "token.yaml".freeze
SCOPE = Google::Apis::CalendarV3::AUTH_CALENDAR

##
# Ensure valid credentials, either by restoring from the saved credentials
# files or intitiating an OAuth2 authorization. If authorization is required,
# the user's default browser will be launched to approve the request.
#
# @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
def authorize
  client_id = Google::Auth::ClientId.from_file CREDENTIALS_PATH
  token_store = Google::Auth::Stores::FileTokenStore.new file: TOKEN_PATH
  authorizer = Google::Auth::UserAuthorizer.new client_id, SCOPE, token_store
  user_id = "default"
  credentials = authorizer.get_credentials user_id
  if credentials.nil?
    url = authorizer.get_authorization_url base_url: OOB_URI
    puts "Open the following URL in the browser and enter the " \
         "resulting code after authorization:\n" + url
    code = gets
    credentials = authorizer.get_and_store_credentials_from_code(
      user_id: user_id, code: code, base_url: OOB_URI
    )
  end
  credentials
end

# Initialize the API
service = Google::Apis::CalendarV3::CalendarService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize

event = Google::Apis::CalendarV3::Event.new(
  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: Google::Apis::CalendarV3::EventDateTime.new(
    date_time: '2015-05-28T09:00:00-07:00',
    time_zone: 'America/Los_Angeles'
  ),
  end: Google::Apis::CalendarV3::EventDateTime.new(
    date_time: '2015-05-28T17:00:00-07:00',
    time_zone: 'America/Los_Angeles'
  ),
  recurrence: [
    'RRULE:FREQ=DAILY;COUNT=2'
  ],
  attendees: [
    Google::Apis::CalendarV3::EventAttendee.new(
      email: 'lpage@example.com'
    ),
    Google::Apis::CalendarV3::EventAttendee.new(
      email: 'sbrin@example.com'
    )
  ],
  reminders: Google::Apis::CalendarV3::Event::Reminders.new(
    use_default: false,
    overrides: [
      Google::Apis::CalendarV3::EventReminder.new(
        reminder_method: 'email',
        minutes: 24 * 60
      ),
      Google::Apis::CalendarV3::EventReminder.new(
        reminder_method: 'popup',
        minutes: 10
      )
    ]
  )
)

result = service.insert_event('primary', event)
puts "Event created: #{result.html_link}"

Gemfile.lock

GEM
  remote: https://rubygems.org/
  specs:
    activesupport (6.1.3)
      concurrent-ruby (~> 1.0, >= 1.0.2)
      i18n (>= 1.6, < 2)
      minitest (>= 5.1)
      tzinfo (~> 2.0)
      zeitwerk (~> 2.3)
    addressable (2.7.0)
      public_suffix (>= 2.0.2, < 5.0)
    concurrent-ruby (1.1.8)
    declarative (0.0.20)
    declarative-option (0.1.1)
    faraday (1.3.0)
      faraday-net_http (~> 1.0)
      multipart-post (>= 1.2, < 3)
      ruby2_keywords
    faraday-net_http (1.0.1)
    gems (1.2.0)
    google-api-client (0.53.0)
      google-apis-core (~> 0.1)
      google-apis-generator (~> 0.1)
    google-apis-core (0.3.0)
      addressable (~> 2.5, >= 2.5.1)
      googleauth (~> 0.14)
      httpclient (>= 2.8.1, < 3.0)
      mini_mime (~> 1.0)
      representable (~> 3.0)
      retriable (>= 2.0, < 4.0)
      rexml
      signet (~> 0.14)
      webrick
    google-apis-discovery_v1 (0.2.0)
      google-apis-core (~> 0.1)
    google-apis-generator (0.2.0)
      activesupport (>= 5.0)
      gems (~> 1.2)
      google-apis-core (~> 0.1)
      google-apis-discovery_v1 (~> 0.0)
      thor (>= 0.20, < 2.a)
    googleauth (0.16.0)
      faraday (>= 0.17.3, < 2.0)
      jwt (>= 1.4, < 3.0)
      memoist (~> 0.16)
      multi_json (~> 1.11)
      os (>= 0.9, < 2.0)
      signet (~> 0.14)
    httpclient (2.8.3)
    i18n (1.8.9)
      concurrent-ruby (~> 1.0)
    jwt (2.2.2)
    memoist (0.16.2)
    mini_mime (1.0.2)
    minitest (5.14.4)
    multi_json (1.15.0)
    multipart-post (2.1.1)
    os (1.1.1)
    public_suffix (4.0.6)
    representable (3.0.4)
      declarative (< 0.1.0)
      declarative-option (< 0.2.0)
      uber (< 0.2.0)
    retriable (3.1.2)
    rexml (3.2.4)
    ruby2_keywords (0.0.4)
    signet (0.15.0)
      addressable (~> 2.3)
      faraday (>= 0.17.3, < 2.0)
      jwt (>= 1.5, < 3.0)
      multi_json (~> 1.10)
    thor (1.1.0)
    tzinfo (2.0.4)
      concurrent-ruby (~> 1.0)
    uber (0.1.0)
    webrick (1.7.0)
    zeitwerk (2.4.2)

PLATFORMS
  ruby

DEPENDENCIES
  google-api-client

BUNDLED WITH
   2.1.4

所以我们在 google 存储 api 更新到 1.31 后遇到了同样的问题。所以我们将其降级为 1.29.1。请也尝试降级您的版本,它可能会起作用!

我们需要清除我们的 heroku 缓存并重新安装所有的 gem。

https://help.heroku.com/18PI5RSY/how-do-i-clear-the-build-cache

希望对您有所帮助