前提・実現したいこと
Webアプリで、データベースより取得した項目を、googleスプレッドシートへ書込みをしたいと思います。
teratailでも同じような案件で使用されていますGoogleのチュートリアル
https://developers.google.com/sheets/api/quickstart/java
のコードを参考しています。
java
1import java.io.FileInputStream; 2import java.io.IOException; 3import java.io.InputStream; 4import java.io.InputStreamReader; 5import java.security.GeneralSecurityException; 6import java.util.Arrays; 7import java.util.Collections; 8import java.util.List; 9 10import com.google.api.client.auth.oauth2.Credential; 11import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; 12import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; 13import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; 14import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; 15import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 16import com.google.api.client.http.javanet.NetHttpTransport; 17import com.google.api.client.json.JsonFactory; 18import com.google.api.client.json.jackson2.JacksonFactory; 19import com.google.api.client.util.store.FileDataStoreFactory; 20import com.google.api.services.sheets.v4.Sheets; 21import com.google.api.services.sheets.v4.SheetsScopes; 22import com.google.api.services.sheets.v4.model.UpdateValuesResponse; 23import com.google.api.services.sheets.v4.model.ValueRange; 24import com.qs.web.common.model.UseItem; 25 26public class GSheetReadWriter { 27 private static final String APPLICATION_NAME = "Sample using QuickStart"; 28 private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); 29 30 private static final List<String> SCOPES = Collections.singletonList(SheetsScopes.SPREADSHEETS); 31 32 private Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT,UseItem USE_ITEM) throws IOException { 33 InputStream in = new FileInputStream(credentialsFilePath); 34 GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); 35 java.io.File TOKENS_DIR_PATH = new java.io.File(tokensDirectoryPath); 36 GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( 37 HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) 38 .setDataStoreFactory(new FileDataStoreFactory(TOKENS_DIR_PATH)) 39 .setAccessType("offline") 40 .setApprovalPrompt("auto") 41 .build(); 42 LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build(); 43 return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); 44 45 } 46 47 private String credentialsFilePath = "/var/qs/credentials.json"; 48 private String tokensDirectoryPath = "/var/qs/tokens"; 49 private String spreadsheetId; 50 private Sheets service; 51 52 /* 53 * コンストラクタ 54 */ 55 public GSheetReadWriter(String credentialsFilePath,String tokensDirectoryPath,String spreadsheetId,UseItem USE_ITEM) 56 throws GeneralSecurityException, IOException { 57 this.credentialsFilePath = credentialsFilePath; 58 this.tokensDirectoryPath = tokensDirectoryPath; 59 final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); 60 this.spreadsheetId = spreadsheetId; 61 this.service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY 62 , getCredentials(HTTP_TRANSPORT,USE_ITEM)) 63 .setApplicationName(APPLICATION_NAME) 64 .build(); 65 } 66 /* 67 * spreadsheet の指定 range の行に、複数のデータ (row) を書き込み 68 */ 69 public void writeRows(String range, List<List<Object>> rows) throws IOException { 70 ValueRange body = new ValueRange().setValues(rows); 71 UpdateValuesResponse result = service.spreadsheets().values().update(spreadsheetId, range, body) 72 .setValueInputOption("RAW").execute(); 73 System.out.println(result); 74 } 75}
発生している問題
上記のコードをローカル環境のPCでEclipseより実行している際は、googleスプレッドシートへ書込みができます。
初回の実行では、ブラウザへ”Received verification code. You may now close this window.”
が表示され、指定したディレクトリへ”StoredCredential”の名称でトークンが書き込まれて
おり、2回目以降の実行では、ブラウザへ特別表示はありませんが、スプレッドシート
への書き込みは正常にできます。
上記と同じものをクラウドVPSへ配備して実行すると、初回の実行でもブラウザへ
”Received verification code. You may now close this window.”は無く、書込み
もされていません。但し、”StoredCredential”の名称でトークンらしきものは書き込まれて
いますが、ローカル環境PCのそれとは内容が異なっていました。
試しに、ローカル環境のトークン”StoredCredential”をクラウドVPSの所定のディレクトリ
へアップロードしてから実行しましたら、スプレッドシートへ書込みが可能となります。
クラウドVPS上でアプリを実行した際、作成されるトークン”StoredCredential”に問題が
あるようでGoogleスレッドシートへの書き込みがされないようですが原因が分かりません。
基本的なところで勘違いをしているかもしれません。
ご教示頂ければ幸いです。
よろしくお願いします。
補足情報(FW/ツールのバージョンなど)
GMOのクラウドVPS(OSはCentOS7)で試しています。
jdk1.8
tomcat7
あなたの回答
tips
プレビュー