Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google sheets Java API cannot find client_secrets?

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

like image 684
JCWong Avatar asked Aug 27 '17 05:08

JCWong


People also ask

How to use Google Sheets API in Java?

Java Quickstart 1 Turn on the Google Sheets API. In resulting dialog click DOWNLOAD CLIENT CONFIGURATION and save the file credentials.json to your working directory. 2 Prepare the project. Copy the credentials.json file you downloaded in Step 1 into the src/main/resources/ directory you just created. 3 Set up the sample. ... 4 Run the sample. ...

What are Google APIs client_ID and Client_Secret Files?

The Google APIs client library for .NET uses client_secrets.json files for storing the client_id, client_secret, and other OAuth 2.0 parameters. A client_secrets.json file is a JSON formatted file containing the client ID, client secret, and other OAuth 2.0 parameters.

How do I get Google Sheets credentials for my project?

To obtain the credentials, we'll need to create a project in the Google Developers Console and then enable the Google Sheets API for the project. The first step in the Google Quickstart guide contains detailed information on how to do this.

How to get authorization for Google Sheets API?

The Google Sheets API requires OAuth 2.0 authorization before we can access it through an application. First, we need to obtain a set of OAuth credentials, then use this in our application to submit a request for authorization. 3.1. Obtaining OAuth 2.0 Credentials


1 Answers

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

like image 126
JCWong Avatar answered Nov 07 '22 10:11

JCWong