![](/img/trans.png)
[英]How to use Google Speech API in an end user c# application securely after Authenticating them with Firebase Auth?
[英]Authenticating Google Cloud Speech via GRPC on C# using an API key
我想在 GRPC 上的 C# 上实现流式语音识别,但只有一个 API KEY
这种类型的密钥 --> https://cloud.google.com/docs/authentication/api-keys
我通过搜索网络知道,在其他语言(android、IOs)中,他们可以“拦截”并添加一个带有如下键的标题:
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;
}
非常感谢
我处于同样的情况,@ Marce,您是否有其他解决方法?
gRPC 允许在每次调用时传递额外的设置。 在您的情况下,以下内容应该可以解决问题:
speech.StreamingRecognize(Google.Api.Gax.Grpc.CallSettings.FromHeader("x-goog-api-key", YOUR_API_KEY));
(注意:您确定它是x-goog-api-key而不是简单的x-api-key吗?)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.