我正在将 ADC 与服务帐户模拟一起使用。当尝试使用 spring-cloud-gcp-pubsub 项目执行此操作时,我收到以下异常:“从流读取凭据时出错,无法识别‘type’值‘impersonated_service_account’。需要‘authorized_user’或‘service_account’。”。
这很奇怪,因为https://github.com/googleapis/google-auth-library-java/blob/8cf273815f197a8b6b3d08043151005456824cbc/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java#L164-L179似乎显示“type”:“impersonated_service_account”应该被识别。
堆栈跟踪
Caused by: java.io.IOException: Error reading credential file from location C:\Users\<my_user>\AppData\Roaming\gcloud\application_default_credentials.json: Error reading credentials from stream, 'type' value 'impersonated_service_account' not recognized. Expecting 'authorized_user' or 'service_account'.
at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentialsUnsynchronized(DefaultCredentialsProvider.java:186) ~[google-auth-library-oauth2-http-0.21.1.jar:na]
at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:126) ~[google-auth-library-oauth2-http-0.21.1.jar:na]
at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:119) ~[google-auth-library-oauth2-http-0.21.1.jar:na]
at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:91) ~[google-auth-library-oauth2-http-0.21.1.jar:na]
at com.google.api.gax.core.GoogleCredentialsProvider.getCredentials(GoogleCredentialsProvider.java:67) ~[gax-1.57.1.jar:1.57.1]
at org.springframework.cloud.gcp.core.DefaultCredentialsProvider.getCredentials(DefaultCredentialsProvider.java:67) ~[spring-cloud-gcp-core-1.2.5.RELEASE.jar:1.2.5.RELEASE]
at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:136) ~[gax-1.57.1.jar:1.57.1]
at com.google.cloud.pubsub.v1.stub.GrpcSubscriberStub.create(GrpcSubscriberStub.java:272) ~[google-cloud-pubsub-1.108.0.jar:1.108.0]
at org.springframework.cloud.gcp.pubsub.support.DefaultSubscriberFactory.createSubscriberStub(DefaultSubscriberFactory.java:278) ~[spring-cloud-gcp-pubsub-1.2.5.RELEASE.jar:1.2.5.RELEASE]
运行此参考项目时发生故障:https://spring.io/guides/gs/messaging-gcp-pubsub/更新到 Java17 和 SpringBoot 3.x 并更新依赖项以考虑 spring-cloud-gcp项目现在属于 com.google.cloud 而不是此处引用的 org.springframework.cloud https://spring.io/projects/spring-cloud-gcp 解决了问题。