teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

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

2020/04/22 14:32

投稿

gureisu
gureisu

スコア13

title CHANGED
File without changes
body CHANGED
@@ -1,8 +1,9 @@
1
1
  JavaでGsuite共有ドライブのスプレッドシートに値を出力したいのですが、アクセスの仕方が分かりません。
2
- apiは、GoogleSheetsApiとGoogleDriveApiを使用しており、マイドライブのスプレッドシートにアクセスすることはできます。また、共有ドライブ内にフォルダを作成し、googleCloudPlatForm・スプレッドシートの共有設定も済んでいます。
2
+ apiは、GoogleSheetsApiとGoogleDriveApiを使用しており、マイドライブのスプレッドシートにアクセスして値を出力することはできます。また、共有ドライブ内にフォルダを作成し、googleCloudPlatForm・スプレッドシートの共有設定も済んでいます。
3
3
  そこで以下についてお聞きしたいです。
4
4
 
5
+ ①現在、サイトを参考に以下のコードで共有ドライブ内のファイルを読み込むか試しています。
5
- ①Javaで共有ドライブにアクセスする、googleDriveApiを使用し、OAuth2.0クライアントID作成時JSONァイとbuild.gradle作成る必要があるのか
6
+   共有ドライブ内のフォルダを認識することできまたがそこからどようにそのダ内のスプレッドシートにアクセスし、処理すのか
6
7
 
7
8
  ②別の方法でアクセスする手段があるのか
8
9
 
@@ -12,20 +13,16 @@
12
13
 
13
14
 
14
15
 
15
- ※ 現在、googleDriveApiの公式リファレンスの以下のコードで共有ドライブ内のファイルを読み込むか試していますが、うまくいきません。(マイドライブのファイルは読み込みます)
16
16
 
17
+
17
18
    (変更点)
18
19
    ・src/main/resourcesにJsonファイルを、プロジェクトディレクトリ配下にbuild.gradleをそれぞれ配置
19
-   ・CREDENTIALS_FILE_PATHを配置したJSONファイル名に変更
20
+   ・CREDENTIALS_FILE_PATHを、OAuth2.0クライアントID作成時のJSONファイル名に変更
20
21
    ・APPLICATION_NAMEは独自で設定したものを使用
21
22
    ・authorize()の引数部分には、実際にアクセスするgoogleアカウントを使用
22
23
    ・SCOPESには全てのファイルを対象とするように追加(https://www.googleapis.com/auth/drive)
23
24
    ・tokensフォルダ内のstoredCredentialはスコープを変更した時に削除
24
25
 
25
-   表示されるエラーです
26
-   (FileDataStoreFactory setPermissionToOwnerOnly
27
- Unable to set permissions for プロジェクトのパス/token, because you are running on a non-POSIX file system.
28
-    SocketException:Socket Closed)
29
26
 
30
27
  ```ここに言語を入力
31
28
  import com.google.api.client.auth.oauth2.Credential;
@@ -40,8 +37,7 @@
40
37
  import com.google.api.client.util.store.FileDataStoreFactory;
41
38
  import com.google.api.services.drive.Drive;
42
39
  import com.google.api.services.drive.DriveScopes;
43
- import com.google.api.services.drive.model.File;
40
+ import s.DriveQuickstart;
44
- import com.google.api.services.drive.model.FileList;
45
41
 
46
42
  import java.io.FileNotFoundException;
47
43
  import java.io.IOException;
@@ -51,7 +47,7 @@
51
47
  import java.util.Collections;
52
48
  import java.util.List;
53
49
 
54
- public class DriveQuickstart {
50
+ public class SharedDrive {
55
51
  private static final String APPLICATION_NAME = "Google Drive API Java Quickstart";
56
52
  private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
57
53
  private static final String TOKENS_DIRECTORY_PATH = "tokens";
@@ -61,13 +57,13 @@
61
57
  * If modifying these scopes, delete your previously saved tokens/ folder.
62
58
  */
63
59
  private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY);
64
- private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
60
+ private static final String CREDENTIALS_FILE_PATH = "/h.json";
65
61
 
66
62
  /**
67
63
  * Creates an authorized Credential object.
68
64
  * @param HTTP_TRANSPORT The network HTTP Transport.
69
65
  * @return An authorized Credential object.
70
- * @throws IOException If the credentials.json file cannot be found.
66
+ * @throws IOException If the cc.json file cannot be found.
71
67
  */
72
68
  private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
73
69
  // Load client secrets.
@@ -84,7 +80,7 @@
84
80
  .setAccessType("offline")
85
81
  .build();
86
82
  LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
87
- return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
83
+ return new AuthorizationCodeInstalledApp(flow, receiver).authorize("dummy@gmail.com");
88
84
  }
89
85
 
90
86
  public static void main(String... args) throws IOException, GeneralSecurityException {
@@ -93,20 +89,13 @@
93
89
  Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
94
90
  .setApplicationName(APPLICATION_NAME)
95
91
  .build();
96
-
92
+ service.teamdrives();
97
93
  // Print the names and IDs for up to 10 files.
98
- FileList result = service.files().list()
94
+ DriveList result = service.drives().list()
99
- .setPageSize(10)
100
- .setFields("nextPageToken, files(id, name)")
101
95
  .execute();
102
- List<File> files = result.getFiles();
96
+ List<com.google.api.services.drive.model.Drive> drives = result.getDrives();
103
- if (files == null || files.isEmpty()) {
97
+ if (drives == null || drives.isEmpty()) {
104
98
  System.out.println("No files found.");
105
- } else {
106
- System.out.println("Files:");
107
- for (File file : files) {
108
- System.out.printf("%s (%s)\n", file.getName(), file.getId());
109
- }
110
99
  }
111
100
  }
112
101
  }

1

コードの追加

2020/04/22 14:32

投稿

gureisu
gureisu

スコア13

title CHANGED
File without changes
body CHANGED
@@ -7,4 +7,107 @@
7
7
  ②別の方法でアクセスする手段があるのか
8
8
 
9
9
  したいことは共有ドライブにアクセスしてワークシートに値を書き込むだけです。
10
+ (eclipseで作成しているjavaのプログラムで、自動テストを実行し、該当のスプレッドシートにアクセスして処理結果を書き込むものです)
10
- ご存知の方いらっしゃいましたら教えていただきたいです。
11
+ ご存知の方いらっしゃいましたら教えていただきたいです。
12
+
13
+
14
+
15
+ ※ 現在、googleDriveApiの公式リファレンスの以下のコードで共有ドライブ内のファイルを読み込むか試していますが、うまくいきません。(マイドライブのファイルは読み込みます)
16
+
17
+   (変更点)
18
+   ・src/main/resourcesにJsonファイルを、プロジェクトディレクトリ配下にbuild.gradleをそれぞれ配置
19
+   ・CREDENTIALS_FILE_PATHを配置したJSONファイル名に変更
20
+   ・APPLICATION_NAMEは独自で設定したものを使用
21
+   ・authorize()の引数部分には、実際にアクセスするgoogleアカウントを使用
22
+   ・SCOPESには全てのファイルを対象とするように追加(https://www.googleapis.com/auth/drive)
23
+   ・tokensフォルダ内のstoredCredentialはスコープを変更した時に削除
24
+
25
+   表示されるエラーです
26
+   (FileDataStoreFactory setPermissionToOwnerOnly
27
+ Unable to set permissions for プロジェクトのパス/token, because you are running on a non-POSIX file system.
28
+    SocketException:Socket Closed)
29
+
30
+ ```ここに言語を入力
31
+ import com.google.api.client.auth.oauth2.Credential;
32
+ import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
33
+ import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
34
+ import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
35
+ import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
36
+ import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
37
+ import com.google.api.client.http.javanet.NetHttpTransport;
38
+ import com.google.api.client.json.JsonFactory;
39
+ import com.google.api.client.json.jackson2.JacksonFactory;
40
+ import com.google.api.client.util.store.FileDataStoreFactory;
41
+ import com.google.api.services.drive.Drive;
42
+ import com.google.api.services.drive.DriveScopes;
43
+ import com.google.api.services.drive.model.File;
44
+ import com.google.api.services.drive.model.FileList;
45
+
46
+ import java.io.FileNotFoundException;
47
+ import java.io.IOException;
48
+ import java.io.InputStream;
49
+ import java.io.InputStreamReader;
50
+ import java.security.GeneralSecurityException;
51
+ import java.util.Collections;
52
+ import java.util.List;
53
+
54
+ public class DriveQuickstart {
55
+ private static final String APPLICATION_NAME = "Google Drive API Java Quickstart";
56
+ private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
57
+ private static final String TOKENS_DIRECTORY_PATH = "tokens";
58
+
59
+ /**
60
+ * Global instance of the scopes required by this quickstart.
61
+ * If modifying these scopes, delete your previously saved tokens/ folder.
62
+ */
63
+ private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY);
64
+ private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
65
+
66
+ /**
67
+ * Creates an authorized Credential object.
68
+ * @param HTTP_TRANSPORT The network HTTP Transport.
69
+ * @return An authorized Credential object.
70
+ * @throws IOException If the credentials.json file cannot be found.
71
+ */
72
+ private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
73
+ // Load client secrets.
74
+ InputStream in = DriveQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
75
+ if (in == null) {
76
+ throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
77
+ }
78
+ GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
79
+
80
+ // Build flow and trigger user authorization request.
81
+ GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
82
+ HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
83
+ .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
84
+ .setAccessType("offline")
85
+ .build();
86
+ LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
87
+ return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
88
+ }
89
+
90
+ public static void main(String... args) throws IOException, GeneralSecurityException {
91
+ // Build a new authorized API client service.
92
+ final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
93
+ Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
94
+ .setApplicationName(APPLICATION_NAME)
95
+ .build();
96
+
97
+ // Print the names and IDs for up to 10 files.
98
+ FileList result = service.files().list()
99
+ .setPageSize(10)
100
+ .setFields("nextPageToken, files(id, name)")
101
+ .execute();
102
+ List<File> files = result.getFiles();
103
+ if (files == null || files.isEmpty()) {
104
+ System.out.println("No files found.");
105
+ } else {
106
+ System.out.println("Files:");
107
+ for (File file : files) {
108
+ System.out.printf("%s (%s)\n", file.getName(), file.getId());
109
+ }
110
+ }
111
+ }
112
+ }
113
+ ```