[英]Get list of all calendars of logged in user (Google calendar API)
I am trying to get all the calendars of the logged in user via oauth google. 我正在尝试通过oauth google获取已登录用户的所有日历。 As of now I can get all the events in the primary calendar but I also want to display all the public calendars of the user.
到目前为止,我可以在主日历中获取所有事件,但我也想显示用户的所有公共日历。
Tried everything but could not find any method to fetch calendars. 尝试了所有方法,但找不到任何方法来获取日历。 This is a ruby on rails 5 app I am working on.
这是我正在使用的Rails 5应用程序上的红宝石。
Code to fetch events of current month 获取当前月份事件的代码
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' })
I tried client.calendarList.list but it shows error "Undefined method calendarList" 我尝试了client.calendarList.list,但显示错误“ Undefined method calendarList”
Thanks for help in advance. 预先感谢您的帮助。
Okay So I have found the solution to this problem and want to share in case someone else comes across same problem. 好的,所以我已经找到了解决此问题的方法,并希望与他人分享,以防其他人遇到相同的问题。
Here is the code 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' })
这是代码
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' })
The problem was that you would need to use calendar_list instead of calendarList. 问题是您将需要使用calendar_list而不是calendarList。
This was the reason it was throwing method not found error. 这就是它抛出方法未找到错误的原因。
Here is the code you will need to initiate google api client. 这是启动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 and oauth_token will be obtained from google after successful oauth login. oauth登录成功后,将从Google获取refresh_token和oauth_token。
ENV["GOOGLE_CLIENT_ID"]
put the client_id obtained from the google while creating app. ENV["GOOGLE_CLIENT_ID"]
放置在创建应用程序时从Google获得的client_id。 ENV["GOOGLE_CLIENT_ID"]
put the client secret. ENV["GOOGLE_CLIENT_ID"]
设置客户端密码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.