我正在尝试在 Google Cloud Platform 上使用 grpc-java (flavor lite) 从 android 进行 GRPC 调用,并且我有来自 nginx esp 的 HTTP 状态代码 400
在我的本地网络上,它可以在 golang 中使用 grpc 服务器。
但是在 Google Cloud Platform 上,我们在 gRPC Google Cloud Endpoints 前面使用 TCP 负载均衡器,并且我们的 golang 后端使用 Google Container Engine 部署。
从我们的第一次分析来看,只有当我们在 grpc-java 上使用带有 JWT 令牌的 grpc 元数据时,它才会出现,如果我们不发送元数据,它就会起作用。
我的端点 config.yaml
type: google.api.Service
config_version: 3
name: auth.endpoints.MYPROJECT.cloud.goog
title: auth gRPC API
apis:
- name: api.AuthApi
usage:
rules:
# Allow unregistered calls for all methods.
- selector: "*"
allow_unregistered_calls: true
我的后端配置
Go version on backend 1.9.1 / 1.9.2
grpc-go version : 1.7.1
protoc version : 3.4.0
我的客户端配置
protoc version : 3.4.0
grpc-java on android : 1.6.1 (i will test with 1.7.0)
Go 代码示例
我们正在使用来自 firebase 的 JWT 令牌和自定义声明,并传递元数据。
// go client sample
md["authorization"] = []string{"bearer " + token}
ctx = metadata.NewOutgoingContext(ctx, md)
** java代码示例**
Metadata headers = new Metadata();
headers.put(TOKEN_KEY, "Bearer " + token);
authClient.attachHeaders(headers);
blockingStub = MetadataUtils.attachHeaders(blockingStub, headers);
我的问题
使用 GCP 上的 Go 客户端,它可以工作。
使用 GCP 上的 grpcc(一个 NodeJS)客户端,它可以工作。
在 android 上使用 grpc-java 失败并显示以下跟踪:
10-26 11:13:49.340 22025-22025/com.mypackage.customer.debug E/AuthenticationManager: Fail to get the custom token from server
io.grpc.StatusRuntimeException: INTERNAL: HTTP status code 400
invalid content-type: text/html
headers: Metadata(:status=400,server=nginx,date=Thu, 26 Oct 2017 09:13:48 GMT,content-type=text/html,content-length=166)
DATA-----------------------------
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
at io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:210)
at io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:191)
at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:124)
at com.mypackage.protobuf.AuthApiGrpc$AuthApiBlockingStub.generateToken(AuthApiGrpc.java:163)
在 Google Cloud Endpoints 上,我可以在我的 esp 上看到此日志:
10.12.0.6 - - [26/Oct/2017:11:13:49 +0000] “-” 400 166 “-” “grpc-java-okhttp/0.0”
代替
10.12.0.6 - - [26/Oct/2017:11:13:49 +0000] "POST /api.AuthApi/generateToken HTTP/2.0" 200 95 "-" "grpc-java-okhttp/0.0"
任何想法 ?