google-cloud-java icon indicating copy to clipboard operation
google-cloud-java copied to clipboard

[translate v3, 2.17.0]: CredentialsProvider is called only once

Open deejay1 opened this issue 2 years ago • 3 comments

Environment details

  1. OS type and version: Ubuntu 22.04
  2. Java version: 17
  3. Version(s): 2.17.0

Steps to reproduce

  1. Get a short lived oauth2 token
  2. Pass it to the TranslateServiceClient
  3. Call translateText once
  4. Wait for token to expire
  5. Call translateText another time
Expected

getCredentials() is either called every time a request is made or an expired token error is received

Code example


class TokenBasedGoogleCredentialsProvider(val token: String)
) : CredentialsProvider {
    override fun getCredentials(): Credentials {
        return GoogleCredentials.create(token)
    }
}

val credentials = TokenBasedGoogleCredentialsProvider("SOME_TOKEN_GENERATED_BY_OTHER_MEANS")
val translationServiceSettingsBuilder = TranslationServiceSettings.newBuilder()
translationServiceSettingsBuilder.setCredentialsProvider(credentialsProvider)
val client = TranslationServiceClient.create(translationServiceSettingsBuilder.build())

client.translateText("project/some-project", "cs", listOf("Hello"))
# Wait until token expires
client.translateText("project/some-project", "cs", listOf("Hello"))

Stack trace

Caused by: io.grpc.StatusRuntimeException: UNAUTHENTICATED: Failed computing credential metadata
	at io.grpc.Status.asRuntimeException(Status.java:539)
	... 14 common frames omitted
Caused by: java.lang.IllegalStateException: OAuth2Credentials instance does not support refreshing the access token. An instance with a new access token should be used, or a derived type that supports refreshing.
	at com.google.auth.oauth2.OAuth2Credentials.refreshAccessToken(OAuth2Credentials.java:366)
	at com.google.auth.oauth2.OAuth2Credentials$1.call(OAuth2Credentials.java:269)
	at com.google.auth.oauth2.OAuth2Credentials$1.call(OAuth2Credentials.java:266)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at com.google.auth.oauth2.OAuth2Credentials$RefreshTask.run(OAuth2Credentials.java:633)

deejay1 avatar May 22 '23 13:05 deejay1

Also Storage and BigQuery clients seem to behave that way, so I have no issues with expiring tokens, because the provider refreshes it by other means instead of a refresh token.

deejay1 avatar May 22 '23 13:05 deejay1

@deejay1 Would you share your observation on Storage and BigQuery clients? (such as error messages)

Also Storage and BigQuery clients seem to behave that way

suztomo avatar May 22 '23 15:05 suztomo

@deejay1 Can you provide some references to documentation that informed your expectation for when getCredentials() is supposed to be called?

cc: @TimurSadykov

meltsufin avatar May 22 '23 18:05 meltsufin

Closing due to lack of updates from the OP.

meltsufin avatar Mar 20 '24 17:03 meltsufin