简体   繁体   English

阅读Google电子表格

[英]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.

相关问题 在不使用任何 Java 凭据的情况下阅读 Google 电子表格 - Reading Google spread sheet without using any credential in Java 谁能帮我从JAVA中的Google电子表格读取数据 - can anyone help me with reading data from google spread sheet in JAVA 如何动态获取谷歌电子表格的范围 - How to get range of google spread sheet dynamically 如何从Google Spreadsheet获取单元格值,该值可以使用Java中的任何语言和Spreadsheet当前时间 - How to get cell value from google spread sheet which in any language and current time of spread sheet in JAVA Google Drive API(java):将数据表/图表添加到电子表格(SpreadsheetEntry) - Google Drive API (java): Add a datatable/chart to a spread sheet (SpreadsheetEntry) 403 - 使用 Java 将数据写入 Google 电子表格的权限不足 - 403 - Insufficient Permission to write data to Google spread sheet using Java 使用 api 读写谷歌电子表格是免费或付费的 - read write to google spread sheet using api is free or paid 按下菜单按钮时,Android,Google Spread Sheet崩溃 - Android, Google Spread Sheet crashes when on menu button is pressed 如何使用Java和Drive API在Google Drive上创建电子表格 - How to create a Spread Sheet on Google Drive Using Java and Drive API 使用google电子表格api时如何解决“java.lang.NoSuchMethodError:” - how to solve "java.lang.NoSuchMethodError:" while working with google spread sheet api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM