5

我正在使用 Swift gRPC 库(奇怪的是它没有在 gRPC 的网站上列出,但有一个GitHub存储库)来构建适用于 macOS 的 Google Assistant SDK 的实现。我已经获得了我的 OAuth2 凭据和令牌,并且正在尝试发出初始请求以开始对话,但是它没有这样做。

我总是得到错误Google_Assistant_Embedded_V1Alpha1_EmbeddedAssistantClientError error 1.)gRPC.CallError error 1.

我运行 Wireshark 来尝试调试问题,我看到我的计算机正在尝试建立连接,但最终中止了连接。我认为这可能是由于 TLS 问题,但我不确定是否真的是这种情况或如何解决它。

我注意到服务初始化函数在您指定证书的地方有一个重载,但我不知道该放什么(或者是否需要使用该函数)

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)")
    }
}

如果有帮助的话,这就是 Wireshark 的样子: enter image description here

4

4 回答 4

3

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])
于 2017-05-09T03:03:27.203 回答
1

Please see some new examples in the grpc-swift project that call Google Cloud APIs. I've simplified client creation to use TLS by default and to embed a default roots.pem in the library binary. You can see that here along with usage of an auth library that supports Google Application Default Credentials.

于 2018-01-03T16:19:58.083 回答
0

我不了解 Swift,您的 Wireshark 屏幕截图可能掩盖了重要信息,但我相信以下一项或两项可能是问题所在:

  1. Constants.ASSISTANT_API_ENDPOINT需要设置为“embeddedassistant.googleapis.com”。(不清楚,但我不认为 Wireshark 中显示的目标地址是 API 端点,它应该是。)

  2. Constants.AUTH_TOKEN_KEY should be the auth token that you've gotten as part of the OAuth2 dance. Keep in mind that it expires (usually after an hour) and you'll need to get a new one. I don't know how Swift configuration works for it, but in others you don't need to specify the "Bearer" part of the auth information since you're not passing the header specifically.

于 2017-05-06T11:47:36.750 回答
0

Took the latest
pod 'SwiftGRPC' pod 'SwiftProtobuf' version using pod update and it got fixed .

Installing BoringSSL 10.0.6 (was 10.0.5 and source changed to https://github.com/CocoaPods/Specs.git from https://github.com/cocoapods/specs.git) Installing SwiftGRPC 0.5.1 (was 0.4.3 and source changed to https://github.com/CocoaPods/Specs.git from https://github.com/cocoapods/specs.git) Installing gRPC-Core 1.12.0 (was 1.11.0 and source changed to https://github.com/CocoaPods/Specs.git from https://github.com/cocoapods/specs.git) Installing Realm 3.7.6 (was 3.7.4 and source changed to https://github.com/CocoaPods/Specs.git from https://github.com/cocoapods/specs.git) Installing RealmSwift 3.7.6 (was 3.7.4 and source changed to https://github.com/CocoaPods/Specs.git from https://github.com/cocoapods/specs.git)

Then all i need was create the session/service with my certificate.

let certPath = Bundle.main.url(forResource: "XYZ", withExtension: "pem")! let certificate = try! String(contentsOf: certPath) service = Pronounce_PronounceServiceClient(address: Constants.ServerApi.grpcBaseUrl, certificates: certificate)

于 2018-08-28T10:20:20.890 回答