質問編集履歴
1
試してみたことを追記しました
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
ログイン時にGoogleカレンダーの予定を取得したいです(rails)
|
test
CHANGED
@@ -8,9 +8,7 @@
|
|
8
8
|
|
9
9
|
```
|
10
10
|
|
11
|
-
|
11
|
+
uninitialized constant SessionsController::Google
|
12
|
-
|
13
|
-
undefined method `input_calendar' for #<Class:0x007fe5214385b0>
|
14
12
|
|
15
13
|
```
|
16
14
|
|
@@ -24,7 +22,25 @@
|
|
24
22
|
|
25
23
|
下記のサイトを参考に作りました。
|
26
24
|
|
27
|
-
[https://qiita.com/
|
25
|
+
[https://qiita.com/PharaohKJ/items/ba2a226a2c8173c4bb41](https://qiita.com/PharaohKJ/items/ba2a226a2c8173c4bb41)
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
まず、Google api clientが正常に動いていないと考え
|
30
|
+
|
31
|
+
[こちらの記事](https://web-salad.hateblo.jp/entry/2014/06/28/213040)を参考に再度goggle client api のgemをインストールして入れ直してみたのですが、やはりうまくいっていません。
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
### 開発環境
|
36
|
+
|
37
|
+
Mac
|
38
|
+
|
39
|
+
'rails', '~> 5.2.1'
|
40
|
+
|
41
|
+
'google-api-client', '~> 0.11'
|
42
|
+
|
43
|
+
'omniauth-google-oauth2'
|
28
44
|
|
29
45
|
|
30
46
|
|
@@ -32,189 +48,9 @@
|
|
32
48
|
|
33
49
|
|
34
50
|
|
35
|
-
カレンダーのモデル(calendar.rb)
|
36
|
-
|
37
|
-
```ruby
|
38
|
-
|
39
|
-
c
|
51
|
+
> /config/omniauth.rb
|
40
|
-
|
41
|
-
belongs_to :user
|
42
52
|
|
43
53
|
|
44
|
-
|
45
|
-
def input_calendar
|
46
|
-
|
47
|
-
# Initialize OAuth 2.0 client
|
48
|
-
|
49
|
-
# authorization
|
50
|
-
|
51
|
-
client = Google::APIClient.new(:application_name => '')
|
52
|
-
|
53
|
-
client.authorization.client_id = ENV['GOOGLE_CLIENT_ID']
|
54
|
-
|
55
|
-
client.authorization.client_secret = ENV['GOOGLE_CLIENT_SECRET']
|
56
|
-
|
57
|
-
client.authorization.scope = "https://www.googleapis.com/auth/calendar"
|
58
|
-
|
59
|
-
client.authorization.refresh_token = current_user.refresh_token
|
60
|
-
|
61
|
-
client.authorization.access_token = current_user.access_token
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
cal = client.discovered_api('calendar', 'v3')
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
# イベント取得月の確認
|
70
|
-
|
71
|
-
printf("カレンダーを表示する年(20XX):")
|
72
|
-
|
73
|
-
year = gets.strip.to_i
|
74
|
-
|
75
|
-
printf("カレンダーを表示する月(1-12):")
|
76
|
-
|
77
|
-
month = gets.strip.to_i
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
# 時間を格納
|
82
|
-
|
83
|
-
time_min = Time.utc(year, month, 1, 0).iso8601
|
84
|
-
|
85
|
-
time_max = Time.utc(year, month, 31, 0).iso8601
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
# イベントの取得
|
90
|
-
|
91
|
-
params = {'calendarId' => 'primary',
|
92
|
-
|
93
|
-
'orderBy' => 'startTime',
|
94
|
-
|
95
|
-
'timeMax' => time_max,
|
96
|
-
|
97
|
-
'timeMin' => time_min,
|
98
|
-
|
99
|
-
'singleEvents' => 'True'}
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
result = client.execute(:api_method => cal.events.list,
|
104
|
-
|
105
|
-
:parameters => params)
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
events.each do |event|
|
110
|
-
|
111
|
-
calendar.startTime = event.start.date
|
112
|
-
|
113
|
-
calendar.endTime = event.end.date
|
114
|
-
|
115
|
-
calendar.summary = event.summary
|
116
|
-
|
117
|
-
end
|
118
|
-
|
119
|
-
end
|
120
|
-
|
121
|
-
end
|
122
|
-
|
123
|
-
```
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
セッションコントローラー
|
128
|
-
|
129
|
-
```ruby
|
130
|
-
|
131
|
-
class SessionsController < ApplicationController
|
132
|
-
|
133
|
-
def new
|
134
|
-
|
135
|
-
end
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
def create
|
140
|
-
|
141
|
-
user = User.from_omniauth(request.env["omniauth.auth"])
|
142
|
-
|
143
|
-
calendar = Calendar.input_calendar
|
144
|
-
|
145
|
-
if user.save
|
146
|
-
|
147
|
-
session[:user_id] = user.id
|
148
|
-
|
149
|
-
redirect_to root_path
|
150
|
-
|
151
|
-
else
|
152
|
-
|
153
|
-
redirect_to new_session_path
|
154
|
-
|
155
|
-
end
|
156
|
-
|
157
|
-
end
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
def destroy
|
162
|
-
|
163
|
-
session[:user_id] = nil
|
164
|
-
|
165
|
-
redirect_to new_session_path
|
166
|
-
|
167
|
-
end
|
168
|
-
|
169
|
-
end
|
170
|
-
|
171
|
-
```
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
ユーザーモデル
|
176
|
-
|
177
|
-
```Ruby
|
178
|
-
|
179
|
-
class User < ApplicationRecord
|
180
|
-
|
181
|
-
has_many :calendars
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
def self.from_omniauth(auth)
|
186
|
-
|
187
|
-
where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
|
188
|
-
|
189
|
-
user.provider = auth.provider
|
190
|
-
|
191
|
-
user.uid = auth.uid
|
192
|
-
|
193
|
-
user.name = auth.info.name
|
194
|
-
|
195
|
-
user.email = auth.info.email
|
196
|
-
|
197
|
-
user.image = auth.info.image
|
198
|
-
|
199
|
-
user.access_token = auth.credentials.token
|
200
|
-
|
201
|
-
user.refresh_token = auth.credentials.refresh_token
|
202
|
-
|
203
|
-
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
|
204
|
-
|
205
|
-
return user
|
206
|
-
|
207
|
-
end
|
208
|
-
|
209
|
-
end
|
210
|
-
|
211
|
-
end
|
212
|
-
|
213
|
-
```
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
認証(omniauth.rb)
|
218
54
|
|
219
55
|
```Ruby
|
220
56
|
|
@@ -222,21 +58,21 @@
|
|
222
58
|
|
223
59
|
provider :google_oauth2,
|
224
60
|
|
225
|
-
|
61
|
+
Rails.application.credentials.google[:google_client_id],
|
226
62
|
|
227
|
-
|
63
|
+
Rails.application.credentials.google[:google_client_secret],
|
228
64
|
|
229
65
|
{
|
230
66
|
|
231
|
-
|
67
|
+
:provider_ignores_state => true,
|
232
68
|
|
233
|
-
|
69
|
+
image_size: 150,
|
234
70
|
|
235
|
-
|
71
|
+
scope: %w(https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/calendar).join(' '),
|
236
72
|
|
237
|
-
prompt:
|
73
|
+
prompt: 'consent',
|
238
74
|
|
239
|
-
access_type:
|
75
|
+
access_type: 'offline'
|
240
76
|
|
241
77
|
}
|
242
78
|
|
@@ -246,22 +82,64 @@
|
|
246
82
|
|
247
83
|
|
248
84
|
|
249
|
-
|
85
|
+
> app/controllers/sessions_controller.rb
|
250
86
|
|
251
87
|
```Ruby
|
252
88
|
|
253
|
-
class
|
89
|
+
class SessionsController < ApplicationController
|
254
90
|
|
91
|
+
・・・
|
92
|
+
|
93
|
+
def create
|
94
|
+
|
95
|
+
@user = User.from_omniauth(request.env["omniauth.auth"])
|
96
|
+
|
97
|
+
secrets = Google::APIClient::ClientSecrets.new(
|
98
|
+
|
99
|
+
web: {
|
100
|
+
|
255
|
-
|
101
|
+
access_token: @user.credentials.token,
|
102
|
+
|
103
|
+
refresh_token: @user.credentials.refresh_token,
|
104
|
+
|
105
|
+
client_id: Rails.application.credentials.google[:google_client_id],
|
106
|
+
|
107
|
+
client_secret: Rails.application.credentials.google[:google_client_secret]
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
)
|
256
112
|
|
257
113
|
|
258
114
|
|
259
|
-
|
115
|
+
cal_service = Google::Apis::CalendarV3::CalendarService.new
|
260
116
|
|
117
|
+
cal_service.authorization = secrets.to_authorization
|
118
|
+
|
119
|
+
@calendar_list = cal_service.list_calendar_lists
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
if @user.save
|
124
|
+
|
261
|
-
|
125
|
+
session[:user_id] = @user.id
|
126
|
+
|
127
|
+
render json: @calendar_list
|
128
|
+
|
129
|
+
else
|
130
|
+
|
131
|
+
render json: "fail to login.\n", status: 500
|
132
|
+
|
133
|
+
end
|
262
134
|
|
263
135
|
end
|
264
136
|
|
265
|
-
|
137
|
+
・・・
|
266
138
|
|
267
139
|
```
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
Googleカレンダーを取得してくるアクションをメソッド化して、別のパターンでも取ってこれるようにするのが理想です。
|
144
|
+
|
145
|
+
欲しい情報は、各イベントの「開始日時」、「終了日時」のみです。すみませんが、おたすけくださいませ。
|