繁体   English   中英

阅读Google电子表格

[英]Reading Google spread sheet

我正在尝试使用Java代码阅读Google SpreadSheet。 为此,我下载了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;
    }

上面的代码工作正常。 在这里,我将client_secret.json文件放在src / main / resources /路径中。

但是现在我想从如下的自定义路径中读取client_secret.json:

GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
                new InputStreamReader(SpreadSheetReader.class.getResourceAsStream("D:/test/client_secret.json")));  

但是,当我更改为其他路径时,尽管我给出了正确的绝对路径,但在上面的确切行中却遇到了“ NullPointerException”。

如果有人遇到类似问题,请您能帮我吗?

SpreadSheetReader.class.getResourceAsStream("D:/test/client_secret.json") -很可能是导致NPE的原因,因为getResourceAsStream在类路径中(基本上在JAR内部)寻找资源。

D:/绝对不在类路径中,因此我建议您改用FileInputStream

... new InputStreamReader(new FileInputStream("D:/test/.."));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM