2016-06-24 90 views
0

我正在尝试通过oauth google获取登录用户的所有日历。截至目前,我可以获取主日历中的所有事件,但我也想显示用户的所有公共日历。获取登录用户的所有日历列表(Google日历API)

尝试了一切,但找不到任何方法来提取日历。这是我正在开发的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”

感谢提前的帮助。

+0

就像在这[post](http://stackoverflow.com/questions/28772554/google-calendar-api-cant-list-events-from-secondary-calendar)中,其他日历有自己的ID。日历API没有任何可合并不同日历的功能。因此,一种解决方案是,使用[CalendarList:list](https://developers.google.com/google-apps/calendar/v3/reference/calendarList/list)返回用户日历列表中的条目,然后您可以遍历他们每个人的事件。 [源](http://stackoverflow.com/questions/35169118/how-to-get-all-events-from-google-calendar-using-google-calendar-api) – KENdi

回答

1

好吧所以我找到了这个问题的解决方案,并希望分享以防别人遇到同样的问题。

下面是代码 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' })

的问题是,你需要改用calendarList的calendar_list。

这就是它抛出方法未找到错误的原因。

这里是您需要启动谷歌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将在成功登录oauth后从google获得。

ENV["GOOGLE_CLIENT_ID"]把从谷歌获得client_id创建应用程序时。 ENV["GOOGLE_CLIENT_ID"]把客户端的秘密。

相关问题