2017-08-27 58 views
8

我正在關注tutorial,它顯示瞭如何使用Java連接到谷歌圖表。它使用gradle這個來獲取依賴這樣Google工作表Java API無法找到client_secrets?

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的工具欄中,我進入了文件 - >項目結構 - >依賴關係,並添加了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"); 

值爲null中的IntelliJ運行的時候,但在終端

編輯不運行時:這是項目樹的樣子

enter image description here

+0

[idea.log](https:// intellij-support)中的任何錯誤.jetbrains.com/hc/articles/207241085)與導入Gradle項目相關? – CrazyCoder

+0

它是從命令行構建的嗎? –

+0

gradle構建成功,是的 – JCWong

回答

4

看來你只需右鍵單擊資源目錄,然後告訴IntelliJ將其標記爲Resources根