[英]Reading Google spread sheet
I am trying to read Google SpreadSheet using java code. 我正在尝试使用Java代码阅读Google SpreadSheet。 For that I have downloaded the client_secret.json file and configured in my java code as below:
为此,我下载了client_secret.json文件并在我的Java代码中进行了如下配置:
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.
在这里,我将client_secret.json文件放在src / main / resources /路径中。
But now I want to read client_secret.json from some custom path like below: 但是现在我想从如下的自定义路径中读取client_secret.json:
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. 但是,当我更改为其他路径时,尽管我给出了正确的绝对路径,但在上面的确切行中却遇到了“ NullPointerException”。
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). SpreadSheetReader.class.getResourceAsStream("D:/test/client_secret.json")
-很可能是导致NPE的原因,因为getResourceAsStream
在类路径中(基本上在JAR内部)寻找资源。
D:/
is definitely not in the classpath, so I suggest you use a FileInputStream
instead: D:/
绝对不在类路径中,因此我建议您改用FileInputStream
:
... new InputStreamReader(new FileInputStream("D:/test/.."));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.