Skip to content

Instantly share code, notes, and snippets.

@E3V3A
Forked from phsultan/testApiKeyAndGrpc.js
Created April 12, 2018 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save E3V3A/07d860c8ce69b8b93658e5ff66306793 to your computer and use it in GitHub Desktop.
Save E3V3A/07d860c8ce69b8b93658e5ff66306793 to your computer and use it in GitHub Desktop.
Google Speech Recognition with API key + gRPC
--- node_modules/@google-cloud/speech/protos/google/cloud/speech/v1/cloud_speech.proto.orig 2018-04-12 11:44:01.000000000 +0200
+++ node_modules/@google-cloud/speech/protos/google/cloud/speech/v1/cloud_speech.proto 2018-04-12 11:44:51.000000000 +0200
@@ -35,7 +35,7 @@
// Performs synchronous speech recognition: receive results after all audio
// has been sent and processed.
rpc Recognize(RecognizeRequest) returns (RecognizeResponse) {
- option (google.api.http) = { post: "/v1/speech:recognize" body: "*" };
+ option (google.api.http) = { post: "/v1/speech:recognize" };
}
// Performs asynchronous speech recognition: receive results via the
@@ -59,6 +59,8 @@
// *Required* The audio data to be recognized.
RecognitionAudio audio = 2;
+
+ string key = 3;
}
// The top-level message sent by the client for the `LongRunningRecognize`
/**
* Make sure you're not already authenticated with gRPC, on MacOS you need
* to ensure the GOOGLE_APPLICATION_CREDENTIALS environment variable is not
* set : `unset GOOGLE_APPLICATION_CREDENTIALS`
*
* @google-cloud/speech and google-gax are used to load the necessary .proto files
*
* I tried to add the 'key' to the request itself, or as a metadata with the request
* and always getting the following error from gRPC :
* Error: 7 PERMISSION_DENIED: The request is missing a valid API key.
*/
var PROTO_PATH = __dirname + '/node_modules/\@google-cloud/speech/protos/google/cloud/speech/v1/cloud_speech.proto';
const grpc = require('grpc');
const fs = require('fs');
const merge = require('lodash.merge');
const gax = require('google-gax');
const path = require('path');
const API_KEY = 'INSERT YOUR API KEY HERE';
const fileName = './audio.raw';
const metadata = new grpc.Metadata();
metadata.add('key', API_KEY);
// Load the applicable protos.
var protos = merge(
{},
gax.grpc().loadProto(
path.join(__dirname, 'node_modules', '\@google-cloud', 'speech', 'protos'),
'google/cloud/speech/v1/cloud_speech.proto'
)
);
// Reads a local audio file and converts it to base64
const file = fs.readFileSync(fileName);
const audioBytes = file.toString('base64');
// The audio file's encoding, sample rate in hertz, and BCP-47 language code
const audio = {
content: audioBytes,
};
const config = {
encoding: 'LINEAR16',
sampleRateHertz: 16000,
languageCode: 'en-US',
};
const request = {
audio: audio,
config: config,
key: API_KEY
};
const client = new protos.google.cloud.speech.v1.Speech('speech.googleapis.com:443', grpc.credentials.createSsl());
client.Recognize(request,
metadata,
function(err, response) {
if (err) {
console.log("ERR :", err);
return;
}
console.log("RESPONSE :", response);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment