質問編集履歴

2

不要な情報を削除

2019/07/12 03:31

投稿

spellbound
spellbound

スコア190

test CHANGED
File without changes
test CHANGED
@@ -122,6 +122,4 @@
122
122
 
123
123
  # 参考
124
124
 
125
- - Python 3.6.5
126
-
127
125
  - Google Calendar API v3

1

コードを追記

2019/07/12 03:31

投稿

spellbound
spellbound

スコア190

test CHANGED
File without changes
test CHANGED
@@ -9,6 +9,98 @@
9
9
  - コードを元に戻したが、アクセスできないまま
10
10
 
11
11
  - docker コンテナを立ち上げ直したりしたが元に戻らず
12
+
13
+
14
+
15
+ # コード
16
+
17
+ ```python
18
+
19
+ from flask import Flask, render_template
20
+
21
+ import datetime
22
+
23
+ import pickle
24
+
25
+ import os.path
26
+
27
+ from googleapiclient.discovery import build
28
+
29
+ from google_auth_oauthlib.flow import InstalledAppFlow
30
+
31
+ from google.auth.transport.requests import Request
32
+
33
+
34
+
35
+ SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
36
+
37
+
38
+
39
+ app = Flask(__name__)
40
+
41
+
42
+
43
+
44
+
45
+ @app.route('/', methods=['GET'])
46
+
47
+ def index():
48
+
49
+ creds = None
50
+
51
+ if os.path.exists('token.pickle'):
52
+
53
+ with open('token.pickle', 'rb') as token:
54
+
55
+ creds = pickle.load(token)
56
+
57
+ if not creds or not creds.valid:
58
+
59
+ if creds and creds.expired and creds.refresh_token:
60
+
61
+ creds.refresh(Request())
62
+
63
+ else:
64
+
65
+ flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
66
+
67
+ creds = flow.run_local_server(port=0)
68
+
69
+ with open('token.pickle', 'wb') as token:
70
+
71
+ pickle.dump(creds, token)
72
+
73
+
74
+
75
+ service = build('calendar', 'v3', credentials=creds)
76
+
77
+
78
+
79
+ now = datetime.datetime.utcnow().isoformat() + 'Z'
80
+
81
+ events_result = service.events().list(calendarId='primary', timeMin=now,
82
+
83
+ maxResults=10, singleEvents=True,
84
+
85
+ orderBy='startTime').execute()
86
+
87
+ events = events_result.get('items', [])
88
+
89
+
90
+
91
+ return render_template('index.html', events=events)
92
+
93
+
94
+
95
+
96
+
97
+ if __name__ == "__main__":
98
+
99
+ app.run(host='0.0.0.0', port=5002, debug=True)
100
+
101
+ ```
102
+
103
+
12
104
 
13
105
 
14
106