Google表格Java API找不到client_secrets?

时间:2017-08-27 05:01:18

标签: java intellij-idea google-sheets google-sheets-api

我正在关注tutorial,其中显示了如何使用Java连接到Google表格。它使用gradle来获取依赖关系

apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'Quickstart'
sourceCompatibility = 1.8
targetCompatibility = 1.8
version = '1.0'

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.google.api-client:google-api-client:1.22.0'
    compile 'com.google.oauth-client:google-oauth-client-jetty:1.22.0'
    compile 'com.google.apis:google-api-services-sheets:v4-rev483-1.22.0'
}

当我从IntelliJ内部运行示例QuickStart.java类时,我可以看到

周围的错误
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.*;
import com.google.api.services.sheets.v4.Sheets;

说找不到图书馆。在IntelliJ的工具栏中,我转到了File - >项目结构 - >依赖关系并添加了build.gradle中的3个项目。之后,围绕进口的错误消失了。当我上课时,我得到了

    objc[1649]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/java (0x102cf14c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x102db94e0). One of the two will be used. Which one is undefined.
Exception in thread "main" java.lang.NullPointerException
    at java.io.Reader.<init>(Reader.java:78)
    at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
    at Quickstart.authorize(Quickstart.java:67)
    at Quickstart.getSheetsService(Quickstart.java:90)
    at Quickstart.main(Quickstart.java:98)

如果我使用gradle -q run在终端中运行它,一切都会完成。不知怎的,

的价值
InputStream in = Quickstart.class.getResourceAsStream("/client_secret.json");
在IntelliJ中运行时,

为null,但在终端

中运行时,

不为空

编辑:这就是项目树的样子

enter image description here

1 个答案:

答案 0 :(得分:4)

似乎您只需要右键单击资源目录,并告诉IntelliJ将其标记为Resources root