forked from shogohida/onebite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickstart.rb
124 lines (116 loc) · 4.56 KB
/
quickstart.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
require "google/apis/calendar_v3"
require "googleauth"
require "googleauth/stores/file_token_store"
require "date"
require "fileutils"
# class AddToGoogleCalendar
OOB_URI = "urn:ietf:wg:oauth:2.0:oob".freeze
APPLICATION_NAME = "Google Calendar API Ruby Quickstart".freeze
CREDENTIALS_PATH = "credentials1.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 = "https://www.googleapis.com/auth/calendar"
# Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY
##
# 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
# rewrite as self.add_events(some arguments), to pass arguments to create events
# def self.add_events(summary, description, date, start_time, end_time, frequency, count)
# Initialize the API
service = Google::Apis::CalendarV3::CalendarService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize
# 1 button to click add a calendar, (connect) with click RUNS THIS CODE, shows the page of calendar
# 2 it gives url to click of calendar BUTTON
# 3 retrive the number from the user to Google calendar
# 4
# pass to hash
# click
# event = Google::Apis::CalendarV3::Event.new(
# summary: summary,
# location: 'At home',
# description: description,
# start: Google::Apis::CalendarV3::EventDateTime.new(
# date_time: "#{date}T#{start_time}+09:00",
# time_zone: 'Asia/Tokyo'
# ),
# end: Google::Apis::CalendarV3::EventDateTime.new(
# date_time: "#{date}T#{end_time[3]}+09:00",
# time_zone: 'Asia/Tokyo'
# ),
# # if frequency[1]
# recurrence: [
# "RRULE:FREQ=#{frequency[0]};COUNT=#{count}#{frequency[1]}"
# ],
# # else
# # recurrence: [
# # "RRULE:FREQ=#{frequency[0]};COUNT=#{count}"
# # ],
# # end
# # attendees: [
# # Google::Apis::CalendarV3::EventAttendee.new(
# # email: '[email protected]'
# # ),
# # Google::Apis::CalendarV3::EventAttendee.new(
# # email: '[email protected]'
# # )
# # ],
# 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
# )
# ]
# )
# )
# # if frequency[1]
# # event.recurrence = "RRULE:FREQ=#{frequency[0]};COUNT=#{count};BYDAY=#{frequency[1]}"
# # else
# # event.recurrence = "RRULE:FREQ=#{frequency[0]};COUNT=#{count}"
# # end
# result = service.insert_event('[email protected]', event)
# primary == my google calendar id
# puts "Event created: #{result.html_link}"
# Fetch the next 10 events for the user
calendar_id = "[email protected]"
response = service.list_events(calendar_id,
max_results: 10,
single_events: true,
order_by: "startTime",
time_min: DateTime.now.rfc3339)
puts "Upcoming events:"
puts "No upcoming events found" if response.items.empty?
response.items.each do |event|
start = event.start.date || event.start.date_time
puts "- #{event.summary} (#{start})"
end
# end
# end