Reputation: 33
So i keep running into these errors when trying to run a sample of a google api project:
dungbui@cloudshell:~ (tiw-leader-project-355316)$ gradle run
> Task :compileJava
/home/dungbui/src/main/java/Create.java:8: error: package com.google.auth.http does not exist
import com.google.auth.http.HttpCredentialsAdapter;
^
/home/dungbui/src/main/java/Create.java:9: error: package com.google.auth.oauth2 does not exist
import com.google.auth.oauth2.GoogleCredentials;
^
/home/dungbui/src/main/java/Create.java:27: error: cannot find symbol
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
^
symbol: class GoogleCredentials
location: class Create
/home/dungbui/src/main/java/Create.java:27: error: cannot find symbol
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
^
symbol: variable GoogleCredentials
location: class Create
/home/dungbui/src/main/java/Create.java:29: error: cannot find symbol
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
^
symbol: class HttpCredentialsAdapter
location: class Create
5 errors
> Task :compileJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Here is my build.gradle file:
And here is my Create.java
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.Spreadsheet;
import com.google.api.services.sheets.v4.model.SpreadsheetProperties;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;
import java.util.Collections;
/* Class to demonstrate the use of Spreadsheet Create API */
public class Create {
/**
* Create a new spreadsheet.
*
* @param title - the name of the sheet to be created.
* @return newly created spreadsheet id
* @throws IOException - if credentials file not found.
*/
public static String createSpreadsheet(String title) throws IOException {
/* Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
.createScoped(Collections.singleton(SheetsScopes.SPREADSHEETS));
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
credentials);
// Create the sheets API client
Sheets service = new Sheets.Builder(new NetHttpTransport(),
GsonFactory.getDefaultInstance(),
requestInitializer)
.setApplicationName("Sheets samples")
.build();
// Create new spreadsheet with a title
Spreadsheet spreadsheet = new Spreadsheet()
.setProperties(new SpreadsheetProperties()
.setTitle(title));
spreadsheet = service.spreadsheets().create(spreadsheet)
.setFields("spreadsheetId")
.execute();
// Prints the new spreadsheet id
System.out.println("Spreadsheet ID: " + spreadsheet.getSpreadsheetId());
return spreadsheet.getSpreadsheetId();
}
}
The Create java i take from the tutorial https://developers.google.com/sheets/api/guides/create
This is my first time trying having a go at google api, can somebody tell me how to fix this.
Upvotes: 2
Views: 3677
Reputation: 2502
I think you are missing the needed Dependency
try adding
implementation 'com.google.auth:google-auth-library-oauth2-http:0.1.0'
to your build.gradle
The error refers to line 27 at /home/dungbui/src/main/java/Create.java
which is declaring GoogleCredentials
.
Upvotes: 4