googleapis / google-cloud-java

Google Cloud Client Library for Java
https://cloud.google.com/java/docs/reference
Apache License 2.0
1.86k stars 1.06k forks source link

Getting null in getAccessToken after giving Service Account Secrets Json #10998

Open RYenike opened 6 days ago

RYenike commented 6 days ago

Environment details

  1. Specify the API at the beginning of the title. For example, "[vision]: ..."). General, Core, and Other are also allowed as types
  2. OS type and version: Windows 10
  3. Java version: 20
  4. Version(s):

Steps to reproduce

Getting null in getAccessToken after giving Service Account Secrets Json

GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(System.getProperty("user.dir") + "\\resources\\json-files\\adc.json"))
        .createScoped("https://www.googleapis.com/auth/cloud-platform");
System.out.println(credentials.getAccessToken());

Code example

GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(System.getProperty("user.dir") + "\\resources\\json-files\\adc.json"))
        .createScoped("https://www.googleapis.com/auth/cloud-platform");
System.out.println(credentials.getAccessToken());

Below is pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.autozone</groupId>
    <artifactId>data-sync</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
       <groupId>com.azo.qa.flex</groupId>
       <artifactId>legacy-parent</artifactId>
       <version>1.1.11-SNAPSHOT</version>
    </parent>

    <properties>
       <maven.compiler.source>1.8</maven.compiler.source>
       <maven.compiler.target>1.8</maven.compiler.target>
       <main.class>com.autozone.datasyncautomation.testrunner.TestRunner</main.class>
    </properties>

    <dependencyManagement>
       <dependencies>
          <dependency>
             <groupId>com.google.cloud</groupId>
             <artifactId>libraries-bom</artifactId>
             <version>26.39.0</version>
             <type>pom</type>
             <scope>import</scope>
          </dependency>
       </dependencies>

    </dependencyManagement>

    <dependencies>
       <dependency>
          <groupId>org.java-websocket</groupId>
          <artifactId>Java-WebSocket</artifactId>
          <version>1.5.0</version>
       </dependency>
       <!-- https://mvnrepository.com/artifact/com.google.cloud/libraries-bom -->

       <dependency>
          <groupId>com.google.cloud</groupId>
          <artifactId>google-cloud-pubsub</artifactId>
          <version>1.122.2</version>
       </dependency>
       <dependency>
          <groupId>com.google.cloud</groupId>
          <artifactId>google-cloud-storage</artifactId>
          <version>2.16.0</version>
       </dependency>
       <!-- https://mvnrepository.com/artifact/com.google.cloud/google-cloud-bigquery -->
       <dependency>
          <groupId>com.google.cloud</groupId>
          <artifactId>google-cloud-bigquery</artifactId>
          <version>2.35.0</version>
       </dependency>
       <!-- https://mvnrepository.com/artifact/com.google.cloud/google-cloud-tasks -->
       <dependency>
          <groupId>com.google.cloud</groupId>
          <artifactId>google-cloud-tasks</artifactId>
       </dependency>

    </dependencies>
    <profiles>
       <profile>
          <id>shade</id>
          <activation>
             <activeByDefault>true</activeByDefault>
          </activation>
          <build>
             <resources>
                <resource>
                   <directory>resources/</directory>
                </resource>
             </resources>
             <plugins>
                <plugin>
                   <groupId>org.apache.maven.plugins</groupId>
                   <artifactId>maven-checkstyle-plugin</artifactId>
                   <executions>
                      <execution>
                         <id>validate</id>
                         <phase>validate</phase>
                      </execution>
                   </executions>
                   <configuration>
                <failOnViolation>false</failOnViolation>
             </configuration>
                </plugin>
                <plugin>
                   <groupId>org.apache.maven.plugins</groupId>
                   <artifactId>maven-shade-plugin</artifactId>
                   <executions>
                      <execution>
                         <id>build</id>
                      </execution>
                   </executions>
                </plugin>
             </plugins>
          </build>
       </profile>
    </profiles>
</project>

Stack trace

Any relevant stacktrace here.

External references such as API reference guides

Any additional information below

Following these steps guarantees the quickest resolution possible.

Thanks!

diegomarquezp commented 4 days ago

Hi @RYenike, thanks for reporting this.

IIUC you are trying to pass your own Application Default Credentials JSON file to create a credentials object, but please be aware that GoogleCredentials has its own getApplicationDefault() method:

GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()

This should be enough to build a client.

Moreover, our clients libraries default to use the Application Default Credentials even if not specified in your code. This can be achieved by running gcloud auth application-default login before running your code.

Please let us know if using the built-in getApplicationDefault or the gcloud command worked for you.

RYenike commented 2 days ago

Hello @diegomarquezp I don't want to use getApplicationDefault , as I will be running my code on another machine which is not having google cloud CLI installed in it, also in specified adc.json they are service account credentials got from secret manager -> Secrets , so I want to use that service account credential to authenticate Please suggest