简体   繁体   中英

Authenticating Google Cloud Speech via GRPC on C# using an API key

i want to implement streaming voice recognition on C# on GRPC, but only have an API KEY

this type of key --> https://cloud.google.com/docs/authentication/api-keys

i know by searching the web that in others languages (android, IOs) they can "intercept" and add a header with the key like:

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

how can i make the same BUT in c#?

my code, if it is neccesary for any reason.

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;
    }

Thank you very much

我处于同样的情况,@ Marce,您是否有其他解决方法?

gRPC allows passing additional settings per-call. In your case, the following should do the trick:

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

(Note: are you sure it's x-goog-api-key and not simply x-api-key ?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM