Android adding authorization header to grpc Stub, Error:"onError: io.grpc.StatusRuntimeException: UNAVAILABLE"

612 views Asked by At

I am unable to add the header successfully using

  // 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) 

as suggested by Android How to add a custom header in grpc client?.

I get: onError: io.grpc.StatusRuntimeException: UNAVAILABLE

in IOS I was able to make it work using:

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)

any advice will be appreciated

1

There are 1 answers

0
Mohammed Aljammali On

the Problem was not in the android client or the attaching of the header.

ALPN was disabled on the backend and it needs to be enforced for android client to work.

I will edit the title of the question to include the error message : onError: io.grpc.StatusRuntimeException: UNAVAILABLE in case someone was facing the Error but was not trying to add a header.