【问题标题】:Authenticating Google Cloud Speech via GRPC on C# using an API key使用 API 密钥在 C# 上通过 GRPC 对 Google Cloud Speech 进行身份验证
【发布时间】:2018-10-01 21:20:33
【问题描述】:

我想在 GRPC 上的 C# 上实现流式语音识别,但只有一个 API KEY

这种类型的密钥 --> https://cloud.google.com/docs/authentication/api-keys

我通过在网上搜索知道,在其他语言(android、IO)中,它们可以“拦截”并添加带有如下键的标题:

Metadata.Key.of("x-goog-api-key", "value");

如何在 c# 中制作相同的 BUT?

我的代码,如果出于任何原因需要的话。

public static async Task<object> StreamingRecognizeAsync(Stream audio)
    {
        var speech = SpeechClient.Create();

        var streamingCall = speech.StreamingRecognize();


        // Write the initial request with the config.
        await streamingCall.WriteAsync(
            new StreamingRecognizeRequest()
            {
                StreamingConfig = new StreamingRecognitionConfig()
                {
                    Config = new RecognitionConfig()
                    {
                        Encoding =
                        RecognitionConfig.Types.AudioEncoding.Linear16,
                        SampleRateHertz = 16000,
                        LanguageCode = "en",
                    },
                    InterimResults = true,
                }
            });
        // Print responses as they arrive.
        Task printResponses = Task.Run(async () =>
        {
            while (await streamingCall.ResponseStream.MoveNext(
                default(CancellationToken)))
            {
                foreach (var result in streamingCall.ResponseStream
                    .Current.Results)
                {
                    foreach (var alternative in result.Alternatives)
                    {
                        Console.WriteLine(alternative.Transcript);
                    }
                }
            }
        });

        var buffer = new byte[32 * 1024];
        int bytesRead;

        while ((bytesRead = await audio.ReadAsync(
                buffer, 0, buffer.Length)) > 0)
        {
            await streamingCall.WriteAsync(
                new StreamingRecognizeRequest()
                {
                    AudioContent = Google.Protobuf.ByteString
                    .CopyFrom(buffer, 0, bytesRead),
                });
            await Task.Delay(500);
        };

        await streamingCall.WriteCompleteAsync();
        await printResponses;
        return 0;
    }

非常感谢

【问题讨论】:

    标签: c# interceptor google-speech-api


    【解决方案1】:

    gRPC 允许在每次调用时传递额外的设置。 在您的情况下,以下应该可以解决问题:

    speech.StreamingRecognize(Google.Api.Gax.Grpc.CallSettings.FromHeader("x-goog-api-key", YOUR_API_KEY));
    

    (注意:你确定它是 x-goog-api-key 而不仅仅是 x-api-key?)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-30
      • 1970-01-01
      • 2019-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      相关资源
      最近更新 更多