I am trying to read Google SpreadSheet using java code. For that I have downloaded the client_secret.json file and configured in my java code as below:
public static Credential authorize() throws IOException {
// Load client secrets.
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
new InputStreamReader(SpreadSheetReader.class.getResourceAsStream("/client_secret.json")));
// 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(service_account);
System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
logger.info("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;
}
Above code is working fine. Here I have placed client_secret.json file in src/main/resources/ path.
But now I want to read client_secret.json from some custom path like below:
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
new InputStreamReader(SpreadSheetReader.class.getResourceAsStream("D:/test/client_secret.json")));
But when I change to some different path I am facing "NullPointerException" in the above exact line though I give correct absolute path.
If anyone has encountered similar issue could you please help me?
SpreadSheetReader.class.getResourceAsStream("D:/test/client_secret.json")
- is most likely what's causing the NPE, since getResourceAsStream
is looking for resources in the classpath (inside your JAR, basically).
D:/
is definitely not in the classpath, so I suggest you use a FileInputStream
instead:
... new InputStreamReader(new FileInputStream("D:/test/.."));