Basically here i am trying to insert google sheet data into oracle table, but code is throwing error always. I have imported all dependencies from maven and credentials.json is in resource folder too, still not able to understand what is the mistake here.
Help me to find the error in code i could have done.
package com.java;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.ValueRange;
public class GoogleSheetsToOracle {
private static final String SPREADSHEET_ID = "1nj5Gia2tkZuiDtH0Wvin2AfWBu5TBQZzi2a5MznZC7Y";
private static final String RANGE = "Expense_Master!B1:C"; // Change to your range
public static void main(String[] args) throws IOException, SQLException, GeneralSecurityException {
// Google Sheets API setup
Credential credential = authorize();
Sheets service = new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(),
GsonFactory.getDefaultInstance(), credential).setApplicationName("Google-Sheets-to-Oracle").build();
// Get data from Google Sheets
ValueRange response = service.spreadsheets().values().get(SPREADSHEET_ID, RANGE).execute();
List<List<Object>> values = response.getValues();
// Oracle database setup
Connection conn = employee.DbConnection.openConnection();
// Insert data into Oracle database
String insertQuery = "INSERT INTO google_sheet (name, price) VALUES (?, ?)";
PreparedStatement pstmt = conn.prepareStatement(insertQuery);
for (List<Object> row : values) {
pstmt.setObject(1, row.get(0));
pstmt.setObject(2, row.get(1));
pstmt.executeUpdate();
}
pstmt.close();
conn.close();
}
private static Credential authorize() throws IOException, GeneralSecurityException {
InputStream in = GoogleSheetsToOracle.class.getResourceAsStream("/credentials.json");
final List<String> SCOPES = Collections.singletonList(SheetsScopes.SPREADSHEETS_READONLY);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(GsonFactory.getDefaultInstance(), new InputStreamReader(in));
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
GoogleNetHttpTransport.newTrustedTransport(), GsonFactory.getDefaultInstance(), clientSecrets, SCOPES).setAccessType("offline").build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
Error –
Exception in thread "main" java.lang.IllegalArgumentException
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:131)
at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:35)
at com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.getDetails(GoogleClientSecrets.java:80)
at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow$Builder.<init>(GoogleAuthorizationCodeFlow.java:211)
at com.java.GoogleSheetsToOracle.authorize(GoogleSheetsToOracle.java:62)
at com.java.GoogleSheetsToOracle.main(GoogleSheetsToOracle.java:34)