googleapis / google-cloud-java

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

Unable to Get API Resource information from API Hub using Java Client Library #11172

Open dsam0162 opened 2 weeks ago

dsam0162 commented 2 weeks ago

I'm using the following libraries to configure the Apihub Java Client to the Gradle project.

However, I'm getting a permission error that is blocking me from invoking any of the ApiHubClient methods. I'm using an access token retrieved from Google Cloud SDK using the command (gcloud auth print-access-token) which has all the necessary permissions to access the APIHub resources. Also, the API Hub Rest APIs are working without issues using the same access token.

Environment details

Java version: 17

Steps to reproduce

(Replace "access-token-here" and put valid values for API name details(project,location,api-id) in the code snippet.)

  1. Create a GoogleCredentials object using an Access token obtained from Google Cloud SDK which has all necessary permissions,
  2. Pass the credentials object as a credential provider for ApiHubSettings
  3. Initialize the ApiHubClient using created ApiHubSettings
  4. Run Java code snippet

Code example

GoogleCredentials credentials = GoogleCredentials.create(AccessToken.newBuilder().setTokenValue("access-token-here").build());

ApiHubSettings apiHubSettings = ApiHubSettings.newBuilder()
    .setCredentialsProvider(()->credentials)
    .build();

//Get Api Resource Details
try (ApiHubClient apiHubClient = ApiHubClient.create(apiHubSettings)) {
   GetApiRequest request =
       GetApiRequest.newBuilder()
           .setName(ApiName.of("xyz-apigee-sbx", "us-central2", "200ab164-97cb-479e-9451-98e42bae1563")
               .toString())
           .build();
   Api response = apiHubClient.getApi(request);
   logger.info("Response: {}",response);
} catch (Exception e) {
   logger.error("An error occurred while processing: {}", e.getMessage(), e);
}

Stack trace

com.google.api.gax.rpc.PermissionDeniedException: io.grpc.StatusRuntimeException: PERMISSION_DENIED: Read access to project 'xyz-apigee-sbx' was denied

External references such as API reference guides

Thanks!

ssvaidyanathan commented 1 day ago

@dsam0162 - I used

ApiHubSettings hubSettings = ApiHubSettings.newBuilder()
                .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
                .build();

and it worked for me

I use it in https://github.com/apigee/apigee-apihub-maven-plugin

dsam0162 commented 1 day ago

Thanks @ssvaidyanathan This fix worked for the following Gradle dependency as well. 🎉🎉🎉

ssvaidyanathan commented 21 hours ago

@dsam0162 - Glad it worked

Can this issue be closed then?