Google sheets Java API cannot find client_secrets?

JCWong :

I am following the tutorial which shows how to connect to google sheets using Java. It uses gradle to get the dependencies as such

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'
}

When I run the example QuickStart.java class from inside IntelliJ, I can see errors around

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;

saying no library found. In IntelliJ's toolbar, I went to File -> Project Structure -> Dependencies and added the 3 items from build.gradle. Afterwards the errors around the imports disappeared. When I run the class I get

    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)

If I run this in terminal using gradle -q run, everything completes. Somehow the value of

InputStream in = Quickstart.class.getResourceAsStream("/client_secret.json");

is null when running in IntelliJ, but not when running in terminal

Edit: This is what the project tree looks like

enter image description here

JCWong :

Seems that you just need to right click on the resources directory, and tell IntelliJ to mark it as Resources root

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=458076&siteId=1