質問編集履歴

2

コードを一部変更したため

2020/04/22 14:32

投稿

gureisu
gureisu

スコア13

test CHANGED
File without changes
test CHANGED
@@ -1,12 +1,14 @@
1
1
  JavaでGsuite共有ドライブのスプレッドシートに値を出力したいのですが、アクセスの仕方が分かりません。
2
2
 
3
- apiは、GoogleSheetsApiとGoogleDriveApiを使用しており、マイドライブのスプレッドシートにアクセスすることはできます。また、共有ドライブ内にフォルダを作成し、googleCloudPlatForm・スプレッドシートの共有設定も済んでいます。
3
+ apiは、GoogleSheetsApiとGoogleDriveApiを使用しており、マイドライブのスプレッドシートにアクセスして値を出力することはできます。また、共有ドライブ内にフォルダを作成し、googleCloudPlatForm・スプレッドシートの共有設定も済んでいます。
4
4
 
5
5
  そこで以下についてお聞きしたいです。
6
6
 
7
7
 
8
8
 
9
+ ①現在、サイトを参考に以下のコードで共有ドライブ内のファイルを読み込むか試しています。
10
+
9
- ①Javaで共有ドライブにアクセスする、googleDriveApiを使用し、OAuth2.0クライアントID作成時JSONァイとbuild.gradle作成る必要があるのか
11
+   共有ドライブ内のフォルダを認識することできまたがそこからどようにそのダ内のスプレッドシートにアクセスし、処理すのか
10
12
 
11
13
 
12
14
 
@@ -26,7 +28,7 @@
26
28
 
27
29
 
28
30
 
29
- ※ 現在、googleDriveApiの公式リファレンスの以下のコードで共有ドライブ内のファイルを読み込むか試していますが、うまくいきません。(マイドライブのファイルは読み込みます)
31
+
30
32
 
31
33
 
32
34
 
@@ -34,7 +36,7 @@
34
36
 
35
37
    ・src/main/resourcesにJsonファイルを、プロジェクトディレクトリ配下にbuild.gradleをそれぞれ配置
36
38
 
37
-   ・CREDENTIALS_FILE_PATHを配置したJSONファイル名に変更
39
+   ・CREDENTIALS_FILE_PATHを、OAuth2.0クライアントID作成時のJSONファイル名に変更
38
40
 
39
41
    ・APPLICATION_NAMEは独自で設定したものを使用
40
42
 
@@ -46,14 +48,6 @@
46
48
 
47
49
 
48
50
 
49
-   表示されるエラーです
50
-
51
-   (FileDataStoreFactory setPermissionToOwnerOnly
52
-
53
- Unable to set permissions for プロジェクトのパス/token, because you are running on a non-POSIX file system.
54
-
55
-    SocketException:Socket Closed)
56
-
57
51
 
58
52
 
59
53
  ```ここに言語を入力
@@ -82,9 +76,7 @@
82
76
 
83
77
  import com.google.api.services.drive.DriveScopes;
84
78
 
85
- import com.google.api.services.drive.model.File;
79
+ import s.DriveQuickstart;
86
-
87
- import com.google.api.services.drive.model.FileList;
88
80
 
89
81
 
90
82
 
@@ -104,7 +96,7 @@
104
96
 
105
97
 
106
98
 
107
- public class DriveQuickstart {
99
+ public class SharedDrive {
108
100
 
109
101
  private static final String APPLICATION_NAME = "Google Drive API Java Quickstart";
110
102
 
@@ -124,7 +116,7 @@
124
116
 
125
117
  private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY);
126
118
 
127
- private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
119
+ private static final String CREDENTIALS_FILE_PATH = "/h.json";
128
120
 
129
121
 
130
122
 
@@ -136,7 +128,7 @@
136
128
 
137
129
  * @return An authorized Credential object.
138
130
 
139
- * @throws IOException If the credentials.json file cannot be found.
131
+ * @throws IOException If the cc.json file cannot be found.
140
132
 
141
133
  */
142
134
 
@@ -170,7 +162,7 @@
170
162
 
171
163
  LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
172
164
 
173
- return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
165
+ return new AuthorizationCodeInstalledApp(flow, receiver).authorize("dummy@gmail.com");
174
166
 
175
167
  }
176
168
 
@@ -188,34 +180,20 @@
188
180
 
189
181
  .build();
190
182
 
191
-
183
+ service.teamdrives();
192
184
 
193
185
  // Print the names and IDs for up to 10 files.
194
186
 
195
- FileList result = service.files().list()
187
+ DriveList result = service.drives().list()
196
-
197
- .setPageSize(10)
198
-
199
- .setFields("nextPageToken, files(id, name)")
200
188
 
201
189
  .execute();
202
190
 
203
- List<File> files = result.getFiles();
191
+ List<com.google.api.services.drive.model.Drive> drives = result.getDrives();
204
-
192
+
205
- if (files == null || files.isEmpty()) {
193
+ if (drives == null || drives.isEmpty()) {
206
194
 
207
195
  System.out.println("No files found.");
208
196
 
209
- } else {
210
-
211
- System.out.println("Files:");
212
-
213
- for (File file : files) {
214
-
215
- System.out.printf("%s (%s)\n", file.getName(), file.getId());
216
-
217
- }
218
-
219
197
  }
220
198
 
221
199
  }

1

コードの追加

2020/04/22 14:32

投稿

gureisu
gureisu

スコア13

test CHANGED
File without changes
test CHANGED
@@ -16,4 +16,210 @@
16
16
 
17
17
  したいことは共有ドライブにアクセスしてワークシートに値を書き込むだけです。
18
18
 
19
+ (eclipseで作成しているjavaのプログラムで、自動テストを実行し、該当のスプレッドシートにアクセスして処理結果を書き込むものです)
20
+
19
21
  ご存知の方いらっしゃいましたら教えていただきたいです。
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+ ※ 現在、googleDriveApiの公式リファレンスの以下のコードで共有ドライブ内のファイルを読み込むか試していますが、うまくいきません。(マイドライブのファイルは読み込みます)
30
+
31
+
32
+
33
+   (変更点)
34
+
35
+   ・src/main/resourcesにJsonファイルを、プロジェクトディレクトリ配下にbuild.gradleをそれぞれ配置
36
+
37
+   ・CREDENTIALS_FILE_PATHを配置したJSONファイル名に変更
38
+
39
+   ・APPLICATION_NAMEは独自で設定したものを使用
40
+
41
+   ・authorize()の引数部分には、実際にアクセスするgoogleアカウントを使用
42
+
43
+   ・SCOPESには全てのファイルを対象とするように追加(https://www.googleapis.com/auth/drive)
44
+
45
+   ・tokensフォルダ内のstoredCredentialはスコープを変更した時に削除
46
+
47
+
48
+
49
+   表示されるエラーです
50
+
51
+   (FileDataStoreFactory setPermissionToOwnerOnly
52
+
53
+ Unable to set permissions for プロジェクトのパス/token, because you are running on a non-POSIX file system.
54
+
55
+    SocketException:Socket Closed)
56
+
57
+
58
+
59
+ ```ここに言語を入力
60
+
61
+ import com.google.api.client.auth.oauth2.Credential;
62
+
63
+ import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
64
+
65
+ import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
66
+
67
+ import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
68
+
69
+ import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
70
+
71
+ import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
72
+
73
+ import com.google.api.client.http.javanet.NetHttpTransport;
74
+
75
+ import com.google.api.client.json.JsonFactory;
76
+
77
+ import com.google.api.client.json.jackson2.JacksonFactory;
78
+
79
+ import com.google.api.client.util.store.FileDataStoreFactory;
80
+
81
+ import com.google.api.services.drive.Drive;
82
+
83
+ import com.google.api.services.drive.DriveScopes;
84
+
85
+ import com.google.api.services.drive.model.File;
86
+
87
+ import com.google.api.services.drive.model.FileList;
88
+
89
+
90
+
91
+ import java.io.FileNotFoundException;
92
+
93
+ import java.io.IOException;
94
+
95
+ import java.io.InputStream;
96
+
97
+ import java.io.InputStreamReader;
98
+
99
+ import java.security.GeneralSecurityException;
100
+
101
+ import java.util.Collections;
102
+
103
+ import java.util.List;
104
+
105
+
106
+
107
+ public class DriveQuickstart {
108
+
109
+ private static final String APPLICATION_NAME = "Google Drive API Java Quickstart";
110
+
111
+ private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
112
+
113
+ private static final String TOKENS_DIRECTORY_PATH = "tokens";
114
+
115
+
116
+
117
+ /**
118
+
119
+ * Global instance of the scopes required by this quickstart.
120
+
121
+ * If modifying these scopes, delete your previously saved tokens/ folder.
122
+
123
+ */
124
+
125
+ private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY);
126
+
127
+ private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
128
+
129
+
130
+
131
+ /**
132
+
133
+ * Creates an authorized Credential object.
134
+
135
+ * @param HTTP_TRANSPORT The network HTTP Transport.
136
+
137
+ * @return An authorized Credential object.
138
+
139
+ * @throws IOException If the credentials.json file cannot be found.
140
+
141
+ */
142
+
143
+ private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
144
+
145
+ // Load client secrets.
146
+
147
+ InputStream in = DriveQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
148
+
149
+ if (in == null) {
150
+
151
+ throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
152
+
153
+ }
154
+
155
+ GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
156
+
157
+
158
+
159
+ // Build flow and trigger user authorization request.
160
+
161
+ GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
162
+
163
+ HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
164
+
165
+ .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
166
+
167
+ .setAccessType("offline")
168
+
169
+ .build();
170
+
171
+ LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
172
+
173
+ return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
174
+
175
+ }
176
+
177
+
178
+
179
+ public static void main(String... args) throws IOException, GeneralSecurityException {
180
+
181
+ // Build a new authorized API client service.
182
+
183
+ final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
184
+
185
+ Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
186
+
187
+ .setApplicationName(APPLICATION_NAME)
188
+
189
+ .build();
190
+
191
+
192
+
193
+ // Print the names and IDs for up to 10 files.
194
+
195
+ FileList result = service.files().list()
196
+
197
+ .setPageSize(10)
198
+
199
+ .setFields("nextPageToken, files(id, name)")
200
+
201
+ .execute();
202
+
203
+ List<File> files = result.getFiles();
204
+
205
+ if (files == null || files.isEmpty()) {
206
+
207
+ System.out.println("No files found.");
208
+
209
+ } else {
210
+
211
+ System.out.println("Files:");
212
+
213
+ for (File file : files) {
214
+
215
+ System.out.printf("%s (%s)\n", file.getName(), file.getId());
216
+
217
+ }
218
+
219
+ }
220
+
221
+ }
222
+
223
+ }
224
+
225
+ ```