Android 向 grpc Stub 添加授权标头,错误:“onError: io.grpc.StatusRuntimeException: UNAVAILABLE”

问题描述

我无法使用

成功添加标题
  // create a custom header
  val header: io.grpc.Metadata = io.grpc.Metadata()
  val auth: io.grpc.Metadata.Key<String> = io.grpc.Metadata.Key.of("authorization",io.grpc.Metadata.ASCII_STRING_MARSHALLER)header.put(auth,"barer "+ AWSMobileClient.getInstance().tokens.idToken.tokenString);
        
     Stub = io.grpc.stub.MetadataUtils.attachHeaders(Stub,header) 

按照Android How to add a custom header in grpc client?的建议。

我得到:onError: io.grpc.StatusRuntimeException: UNAVAILABLE

在 IOS 中,我能够使用:

let authToken = "\(AWSCognitoUserPoolsSignInProvider.sharedInstance().getUserPool().token().result ?? "")"
    let httpHeader: [(String,String)] = [("Authorization",authToken)]
    let header = HPACKHeaders(httpHeader)
    callOptions = CallOptions(customMetadata: header,timeLimit: .none,messageEncoding: .disabled,requestIDProvider: .none,requestIDHeader: nil,cacheable: false)
    let req = Cilix_Api_GetVersionRequest()
    let request = self.client?.getVersion(req,callOptions: callOptions)

任何建议将不胜感激

解决方法

问题不在于android客户端或标题的附加。

ALPN 在后端被禁用,Android 客户端需要强制执行。

我将编辑问题的标题以包含错误消息: onError: io.grpc.StatusRuntimeException: UNAVAILABLE 以防有人遇到错误但没有尝试添加标题。