包com.google.auth.http不存在

发布于 2025-02-13 00:03:53 字数 4408 浏览 3 评论 0原文

因此,在尝试运行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:

build.gradle

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥醉 2025-02-20 00:03:53

我认为您缺少所需的依赖性

尝试添加
实现'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.gradle

The error refers to line 27 at /home/dungbui/src/main/java/Create.java which is declaring GoogleCredentials.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文