包com.google.auth.http不存在
因此,在尝试运行Google API项目的示例时,我一直遇到这些错误:
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
这是我的build.gradle文件:
这是我的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();
}
}
,我从教程 https://develovelers.google.com/sheets/api/guides/create
这是我第一次尝试在Google API上尝试,有人可以告诉我如何解决此问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您缺少所需的依赖性
尝试添加
实现'com.google.auth:google-auth-auth-library-oauth2-http:0.1.0'
to your build.gradle。 /main/java/create.java nate
googlecredentials
。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.gradleThe error refers to line 27 at
/home/dungbui/src/main/java/Create.java
which is declaringGoogleCredentials
.