Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to authenticate a gRPC call for the Assistant SDK?

Tags:

I'm using the Swift gRPC library (which curiously isn't listed on gRPC's website, but there is a GitHub repo for it) to build an implementation of the Google Assistant SDK for macOS. I've gotten my OAuth2 credentials and token, and am trying to make the initial request to begin a conversation, however it fails to do so.

I always get the error Google_Assistant_Embedded_V1Alpha1_EmbeddedAssistantClientError error 1.) and gRPC.CallError error 1.

I ran Wireshark to try and debug the issue, and I saw the my computer is attempting to establish a connection but eventually ends up aborting the connection. I think it may be due to a TLS issue, but I'm not sure if that actually is the case or how to fix it.

I noticed the service initialization function has an overload where you specify certificates, but I don't know what to put there (or if that function needs to be used at all)

typealias AssistantService = Google_Assistant_Embedded_V1Alpha1_EmbeddedAssistantService
typealias AssistantCall = Google_Assistant_Embedded_V1Alpha1_EmbeddedAssistantConverseCall
typealias AudioInConfig = Google_Assistant_Embedded_V1alpha1_AudioInConfig
typealias AudioOutConfig = Google_Assistant_Embedded_V1alpha1_AudioOutConfig
typealias ConverseRequest = Google_Assistant_Embedded_V1alpha1_ConverseRequest
typealias ConverseConfig = Google_Assistant_Embedded_V1alpha1_ConverseConfig    

var service: AssistantService?
var currentCall: AssistantCall?

public init() {
    service = AssistantService(address: Constants.ASSISTANT_API_ENDPOINT)
    let token = "Bearer \(UserDefaults.standard.string(forKey: Constants.AUTH_TOKEN_KEY)!)"
    service?.metadata = Metadata(["authorization" : token])
}

func initiateRequest() {
    var request =   ConverseRequest()
    request.config = ConverseConfig()

    var audioInConfig = AudioInConfig()
    audioInConfig.sampleRateHertz = 16000
    audioInConfig.encoding = .linear16
    request.config.audioInConfig = audioInConfig


    var audioOutConfig = AudioOutConfig()
    audioOutConfig.sampleRateHertz = 16000
    audioOutConfig.encoding = .linear16
    audioOutConfig.volumePercentage = 50
    request.config.audioOutConfig = audioOutConfig

    do {
        currentCall = try service?.converse(completion: { result in
            print("Result code \(result.statusCode)")
            print("Result description \(result.description)")
            print("Metadata \(String(describing: result.initialMetadata))")
            print("Status message \(result.statusMessage ?? "Error")")
            print("Obj description \(String(describing: result))")
            print("result \(result)")
        })

        try currentCall?.send(request) { err in
            print("Error in initial request: \(err)")
        }
    } catch {
        print("Initial error \(error)")
    }
}

This is what the Wireshark for it looks like, if it is any help: enter image description here

like image 586
vanshg Avatar asked May 05 '17 06:05

vanshg


People also ask

Does gRPC require TLS?

The gRPC client must also be configured to not use TLS. For more information, see Call insecure gRPC services with . NET Core client. HTTP/2 without TLS should only be used during app development.

What version of TLS does gRPC use?

gRPC uses TLS1. 3 in c++ by default, this is true as of gRPC version 1.32.

Is gRPC encrypted by default?

When you're using gRPC over a TLS-encrypted HTTP/2 connection, all traffic between clients and servers is encrypted, even if you don't use channel-level authentication. This chapter will show how to apply call credentials and channel credentials to a gRPC service.


1 Answers

I had to add the roots.pem file found here to my project and use it as follows:

let u = Bundle.main.url(forResource: "roots", withExtension: "pem")!
let certificate = try! String(contentsOf: u)
let token = "Bearer \(UserDefaults.standard.string(forKey: Constants.AUTH_TOKEN_KEY)!)"
service = AssistantService(address: Constants.ASSISTANT_API_ENDPOINT, certificates: certificate, host: nil)
service?.metadata = Metadata(["authorization" : token])
like image 100
vanshg Avatar answered Sep 25 '22 09:09

vanshg