java - 从 java 调用 Google Cloud Run

标签 java google-cloud-run

我想从外部应用程序调用 Cloud Run。外部应用程序是用 Kotlin (java) 编写的,并在 JDK 11 JVM 上运行。它使用服务帐户进行身份验证

ServiceAccountCredentials.fromStream(serviceAccount).createScoped("https://www.googleapis.com/auth/cloud-platform")

其中服务帐户所在的是有效的 JSON 文件。我已经测试了在 Google 云控制台内为服务帐户调用云运行的权限。

我还没有找到官方 API,所以我使用 google 的 GoogleNetHttpTransport 来运行 HTTP post 请求。 为了调用云运行,我尝试使用 HttpCredentialsAdapter

transport.createRequestFactory(HttpCredentialsAdapter(googleCredentials))
   .buildPostRequest(GenericUrl(url), JsonHttpContent(JacksonFactory(), data))

我尝试直接使用访问 token

val headers = HttpHeaders()
googleCredentials.refreshIfExpired()
headers.authorization = "Bearer " + googleCredentials.accessToken.tokenValue
postRequest.headers = headers

我尝试使用jet服务帐户并使用jwt请求元数据

val headers = HttpHeaders()
jwtCredentials = ServiceAccountJwtAccessCredentials.fromStream(serviceAccount)
jwtCredentials.getRequestMetadata(URI(url)).forEach {
    headers.set(it.key, it.value)
}
postRequest.headers = headers

在所有这些情况下,它都会返回此错误

[20:35:00 WARN]: Exception in thread "TestThread" com.google.api.client.http.HttpResponseException: 401 Unauthorized
[20:35:00 WARN]:    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1113)
[20:35:00 WARN]:    at ***.CloudRun$invoke$3.invokeSuspend(CloudRun.kt:34)
[20:35:00 WARN]:    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
[20:35:00 WARN]:    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
[20:35:00 WARN]:    at java.base/java.lang.Thread.run(Thread.java:834)

感谢您提前的支持

最佳答案

我花了一些时间研究 OAuth2 Java 库,问题如下。 ServiceAccountCredentials 类添加一个 AccessToken 作为授权,ServiceAccountJwtAccessCredentials 类添加一个自签名 身份 token

我将更深入地讨论它,但现在您可以手动设置标题

        String myUri = "https://.....";

        Credentials credentials =  ServiceAccountCredentials
                .fromStream(resourceLoader.getResource("classpath:./key.json")
                        .getInputStream()).createScoped("https://www.googleapis.com/auth/cloud-platform");

        String token = ((IdTokenProvider)credentials).idTokenWithAudience(myUri, Collections.EMPTY_LIST).getTokenValue();

        HttpRequestFactory factory = new NetHttpTransport().createRequestFactory();
        HttpRequest request = factory.buildGetRequest(new GenericUrl(myUri));
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization","Bearer " + token);
        request.setHeaders(headers);
        HttpResponse httpResponse = request.execute();
        System.out.println(CharStreams.toString(new InputStreamReader(httpResponse.getContent(), Charsets.UTF_8)));

各个部分都在这里,但不能一起工作。

编辑

在 GitHub 上进行一番交流后,Google 为我提供了解决方案。现在 header 会自动配置有签名的 id token

        Credentials credentials =  ServiceAccountCredentials
                .fromStream(resourceLoader.getResource("classpath:./key.json")
                        .getInputStream()).createScoped("https://www.googleapis.com/auth/cloud-platform");


        IdTokenCredentials idTokenCredentials = IdTokenCredentials.newBuilder()
                .setIdTokenProvider((ServiceAccountCredentials)credentials)
                .setTargetAudience(myUri).build();

        HttpRequestFactory factory = new NetHttpTransport().createRequestFactory(new HttpCredentialsAdapter(idTokenCredentials));
        HttpRequest request = factory.buildGetRequest(new GenericUrl(myUri));
        HttpResponse httpResponse = request.execute();
        System.out.println(CharStreams.toString(new InputStreamReader(httpResponse.getContent(), Charsets.UTF_8)));

关于java - 从 java 调用 Google Cloud Run,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60999141/

相关文章:

java - Spring 无法 Autowiring ,有多个 `` 类型的 bean

java - 在二叉搜索树中查找表兄弟

google-cloud-platform - 无法作为服务帐户部署到谷歌云运行

google-cloud-platform - 我如何(或是否可以)使用 Terraform 创建 Google Cloud Run Capacity 设置?

google-cloud-platform - Google Cloud Run 服务 url(发现)

java - {c++} Vertex* myList 和 {java} List myList 有何相似之处?

java - java接收html网页时出错

docker - 上传docker镜像到google云平台 : https://grc. io/v2/: 服务不可用

java - 系统上的多个 JRE

django - 尝试访问 Google Cloud Run 中的容器时出现错误请求 (400)(系统调用不受支持)