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



我正在从Ruby Quickstart复制和粘贴代码,并将其与这里的create-events代码示例相结合。

我唯一改变的是:

  • Google::Apis::CalendarV3::AUTH_CALENDAR_READONLYGoogle::Apis::CalendarV3::AUTH_CALENDAR_READSCOPE
  • 在对.insert_event('primary', event)的调用中从clientservice

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

.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)

这是令人困惑的,尤其是知道它早些时候起作用。这个例子中有什么明显的错误,我没有抓住吗?或者representable或api客户端突然退出了?

人们能够重现这个问题吗?这是我的代码和我的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

所以我们在谷歌存储api更新到1.31后也遇到了同样的问题。因此,我们将其下调至1.29.1。请尝试降级您的版本,它可能会工作!

我们需要清空我们的heroku缓存并重新安装所有的宝石。

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

希望它能帮助

最新更新