I'm putting together a simple application that basically just reads and writes from google sheets (and stores some of that stuff in a postgres database in between operations) using IntelliJ. It all works fine when run locally, except it often (and I can't seem to tell when it "needs" to do this) forces me to open the web browser and validate by logging in.
However, I have to run this (as a .jar) every 10 minutes, and my way of doing this is putting it on an EC2 instance in AWS and using crontab. This means I can't have it forcing me to open up my web browser to validate stuff every time. Is there a way to authenticate without going through this process? The current authentication code looks like this:
private static Credential authorize() throws IOException, URISyntaxException {
// Load client secrets.
InputStream in = SKUListReader.class.getResourceAsStream("/client_secret.json");
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES).setDataStoreFactory(DATA_STORE_FACTORY).setAccessType("offline").build();
Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
System.out.println("Credentials saved to " + dataStoreDir.getAbsolutePath());
return credential;
}