2016-06-24 89 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"]把客戶端的祕密。

相關問題