首页 > 解决方案 > 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建议的那样如何在grpc客户端中添加自定义标头?.

我得到: 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)

任何建议将被认真考虑

标签: androidgrpcgrpc-javagrpc-kotlin

解决方案


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

ALPN 在后端被禁用,需要强制执行才能使 android 客户端正常工作。

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


推荐阅读