質問編集履歴

1

実際に使用したコードを掲載致しました。

2021/05/14 01:39

投稿

itmsd
itmsd

スコア16

test CHANGED
File without changes
test CHANGED
@@ -1,18 +1,174 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- Webアプリより、googleスプレッドシートへ書込みをしたいと思います。
3
+ Webアプリで、データベースより取得した項目を、googleスプレッドシートへ書込みをしたいと思います。
4
4
 
5
5
 
6
6
 
7
7
  teratailでも同じような案件で使用されていますGoogleのチュートリアル
8
8
 
9
+ https://developers.google.com/sheets/api/quickstart/java
10
+
9
- よりJavaクイックスタートのコードを引用しています。
11
+ のコードを参考しています。
12
+
13
+ ```java
14
+
15
+ import java.io.FileInputStream;
16
+
17
+ import java.io.IOException;
18
+
19
+ import java.io.InputStream;
20
+
21
+ import java.io.InputStreamReader;
22
+
23
+ import java.security.GeneralSecurityException;
24
+
25
+ import java.util.Arrays;
26
+
27
+ import java.util.Collections;
28
+
29
+ import java.util.List;
30
+
31
+
32
+
33
+ import com.google.api.client.auth.oauth2.Credential;
34
+
35
+ import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
36
+
37
+ import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
38
+
39
+ import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
40
+
41
+ import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
42
+
43
+ import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
44
+
45
+ import com.google.api.client.http.javanet.NetHttpTransport;
46
+
47
+ import com.google.api.client.json.JsonFactory;
48
+
49
+ import com.google.api.client.json.jackson2.JacksonFactory;
50
+
51
+ import com.google.api.client.util.store.FileDataStoreFactory;
52
+
53
+ import com.google.api.services.sheets.v4.Sheets;
54
+
55
+ import com.google.api.services.sheets.v4.SheetsScopes;
56
+
57
+ import com.google.api.services.sheets.v4.model.UpdateValuesResponse;
58
+
59
+ import com.google.api.services.sheets.v4.model.ValueRange;
60
+
61
+ import com.qs.web.common.model.UseItem;
62
+
63
+
64
+
65
+ public class GSheetReadWriter {
66
+
67
+ private static final String APPLICATION_NAME = "Sample using QuickStart";
68
+
69
+ private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
70
+
71
+
72
+
73
+ private static final List<String> SCOPES = Collections.singletonList(SheetsScopes.SPREADSHEETS);
74
+
75
+
76
+
77
+ private Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT,UseItem USE_ITEM) throws IOException {
78
+
79
+ InputStream in = new FileInputStream(credentialsFilePath);
80
+
81
+ GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
82
+
83
+ java.io.File TOKENS_DIR_PATH = new java.io.File(tokensDirectoryPath);
84
+
85
+ GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
86
+
87
+ HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
88
+
89
+ .setDataStoreFactory(new FileDataStoreFactory(TOKENS_DIR_PATH))
90
+
91
+ .setAccessType("offline")
92
+
93
+ .setApprovalPrompt("auto")
94
+
95
+ .build();
96
+
97
+ LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
98
+
99
+ return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
100
+
101
+
102
+
103
+ }
104
+
105
+
106
+
107
+ private String credentialsFilePath = "/var/qs/credentials.json";
108
+
109
+ private String tokensDirectoryPath = "/var/qs/tokens";
110
+
111
+ private String spreadsheetId;
112
+
113
+ private Sheets service;
114
+
115
+
116
+
117
+ /*
118
+
119
+ * コンストラクタ
120
+
121
+ */
122
+
123
+ public GSheetReadWriter(String credentialsFilePath,String tokensDirectoryPath,String spreadsheetId,UseItem USE_ITEM)
124
+
125
+ throws GeneralSecurityException, IOException {
126
+
127
+ this.credentialsFilePath = credentialsFilePath;
128
+
129
+ this.tokensDirectoryPath = tokensDirectoryPath;
130
+
131
+ final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
132
+
133
+ this.spreadsheetId = spreadsheetId;
134
+
135
+ this.service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY
136
+
137
+ , getCredentials(HTTP_TRANSPORT,USE_ITEM))
138
+
139
+ .setApplicationName(APPLICATION_NAME)
140
+
141
+ .build();
142
+
143
+ }
144
+
145
+ /*
146
+
147
+ * spreadsheet の指定 range の行に、複数のデータ (row) を書き込み
148
+
149
+ */
150
+
151
+ public void writeRows(String range, List<List<Object>> rows) throws IOException {
152
+
153
+ ValueRange body = new ValueRange().setValues(rows);
154
+
155
+ UpdateValuesResponse result = service.spreadsheets().values().update(spreadsheetId, range, body)
156
+
157
+ .setValueInputOption("RAW").execute();
158
+
159
+ System.out.println(result);
160
+
161
+ }
162
+
163
+ }
164
+
165
+ ```
10
166
 
11
167
 
12
168
 
13
169
  ### 発生している問題
14
170
 
15
- ローカル環境のPCでEclipseより実行している際は、問題なく書込みできています。
171
+ 上記のコードをローカル環境のPCでEclipseより実行している際は、googleスプレッドシートへ書込みできます。
16
172
 
17
173
  初回の実行では、ブラウザへ”Received verification code. You may now close this window.”
18
174
 
@@ -20,7 +176,7 @@
20
176
 
21
177
  おり、2回目以降の実行では、ブラウザへ特別表示はありませんが、スプレッドシート
22
178
 
23
- への書き込みは正常にできています。
179
+ への書き込みは正常にできます。
24
180
 
25
181
 
26
182
 
@@ -32,15 +188,15 @@
32
188
 
33
189
  いますが、ローカル環境PCのそれとは内容が異なっていました。
34
190
 
35
- 試しに、ローカル環境の”StoredCredential”をクラウドVPSへアップロードしてから
191
+ 試しに、ローカル環境のトークン”StoredCredential”をクラウドVPSの所定のディレクトリ
36
-
192
+
37
- 実行しましたら、スプレッドシートへ書込みが可能となります。
193
+ へアップロードしてから実行しましたら、スプレッドシートへ書込みが可能となります。
38
-
39
-
40
-
194
+
195
+
196
+
41
- ローカルPCとクラウドVPSで作成される”StoredCredential”がどうして内容が
197
+ クラウドVPSアプリを実行した際、作成されるトークン”StoredCredential”に問題
42
-
198
+
43
- 異なるの原因がません。
199
+ ようでGoogleスレッドシートへ書き込みがされないようですが原因がません。
44
200
 
45
201
 
46
202