我正在尝试通过Oauth Google获取用户的所有日历。截至目前,我可以在主要日历中获得所有事件,但我也想显示用户的所有公共日历。
尝试了所有内容,但找不到任何获取日历的方法。这是我正在处理的Rails 5应用程序的红宝石。
代码获取当前月的事件
response = client.execute(api_method: service.events.list,
parameters: { 'calendarId' => 'primary',
'timeMin': Time.now.beginning_of_month.iso8601,
'showDeleted': false,
'singleEvents': true,
'maxResults': 10,
'orderBy': 'startTime'},
headers: { 'Content-Type' => 'application/json' })
我尝试了client.calendarlist.list,但显示错误"未定义的方法calendarlist"
感谢提前的帮助。
好吧,所以我找到了解决这个问题的解决方案,并想共享以防其他人遇到相同的问题。
这是代码
service = client.discovered_api('calendar' , 'v3')
@response = client.execute(api_method: service.calendar_list.list,
parameters: {'calendarId' => 'secondary'},
'showDeleted': false,
'maxResults': 10,
headers: { 'Content-Type' => 'application/json' })
问题是您需要使用calendar_list而不是calendarlist。
这就是未发现错误的原因。
这是您需要启动Google API客户端的代码。
client = Google::APIClient.new(:auto_refresh_token => true)
client.authorization.access_token = oauth_token
client.authorization.refresh_token = refresh_token
client.authorization.client_id = ENV["GOOGLE_CLIENT_ID"]
client.authorization.client_secret = ENV["GOOGLE_SECRET"]
if client.authorization.refresh_token && client.authorization.expired?
client.authorization.fetch_access_token!
end
Refresh_token和Oauth_token成功后,将从Google获得Oauth登录后。
ENV["GOOGLE_CLIENT_ID"]
在创建应用程序时,将client_id放置在Google中。 ENV["GOOGLE_CLIENT_ID"]
放置客户端秘密。