質問編集履歴

1

切り分け状況の追記

2018/11/08 10:02

投稿

teranotwu
teranotwu

スコア13

test CHANGED
File without changes
test CHANGED
@@ -111,3 +111,131 @@
111
111
 
112
112
 
113
113
  よろしくお願いします。
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+ #追加切り分け ※11/8追記
122
+
123
+ 頂いた回答を元にして、もう少し切り分けしてみました。
124
+
125
+ 結果、OAuth2の認証は動いているようですが、出力結果としては初期のGettingStartのファイルしか出力されていないことが分かりました。なので、やはり、認証関連で何かがおかしいように見えます。。。
126
+
127
+
128
+
129
+
130
+
131
+ ####実行内容
132
+
133
+
134
+
135
+ ```ここに言語を入力
136
+
137
+ #encoding : utf-8
138
+
139
+
140
+
141
+ # Authorize server-to-server interactions from Google Compute Engine.
142
+
143
+ import json
144
+
145
+
146
+
147
+ #1 認証系
148
+
149
+ from google.oauth2 import service_account
150
+
151
+
152
+
153
+ SCOPES = ['https://www.googleapis.com/auth/drive']
154
+
155
+ SERVICE_ACCOUNT_FILE = (サービスアカウントのjsonファイルへのパス)
156
+
157
+
158
+
159
+ credentials = service_account.Credentials.from_service_account_file(
160
+
161
+ SERVICE_ACCOUNT_FILE, scopes=SCOPES)
162
+
163
+
164
+
165
+ print('CREDENTIALs:',credentials.__dict__)
166
+
167
+
168
+
169
+ #2 サービスの構築:実行したいAPI種類を選択し、認証はこのタイミングで実施される
170
+
171
+ from apiclient.discovery import build
172
+
173
+
174
+
175
+ service = build('drive', 'v3', credentials = credentials)
176
+
177
+
178
+
179
+ print('SERVICE:',service.__dict__)\
180
+
181
+
182
+
183
+ page_token = None
184
+
185
+ while True:
186
+
187
+ response = service.files().list(spaces='drive',
188
+
189
+ fields='nextPageToken, files(id, name)',
190
+
191
+ pageToken=page_token).execute()
192
+
193
+
194
+
195
+ print(response.get('nextPageToken'))
196
+
197
+ for file in response.get('files', []):
198
+
199
+ # Process change ; 最初の流儀
200
+
201
+ #print('Found file: %s (%s)' % (file.get('name'), file.get('id')))
202
+
203
+ print(file)
204
+
205
+ page_token = response.get('nextPageToken', None)
206
+
207
+ if page_token is None:
208
+
209
+ break
210
+
211
+
212
+
213
+ ```
214
+
215
+
216
+
217
+ ####実行結果
218
+
219
+
220
+
221
+ ```ここに言語を入力
222
+
223
+ (CREDENTIALS、SERVICE は省略)
224
+
225
+
226
+
227
+ None ←nextPageToken。
228
+
229
+ {'id': (※GoogleDriveのファイルID), 'name': 'Getting started'}
230
+
231
+ ```
232
+
233
+
234
+
235
+
236
+
237
+ ####原因追及
238
+
239
+ こちらが参考になりそうなので、これを元に、もう少し、調査してみようと思います。
240
+
241
+ https://www.daimto.com/google-drive-api-with-a-service-account/