diff --git a/speech/grpc/src/main/java/com/examples/cloud/speech/StreamingRecognizeClient.java b/speech/grpc/src/main/java/com/examples/cloud/speech/StreamingRecognizeClient.java index a7da0e81f1e..d1a529a1216 100644 --- a/speech/grpc/src/main/java/com/examples/cloud/speech/StreamingRecognizeClient.java +++ b/speech/grpc/src/main/java/com/examples/cloud/speech/StreamingRecognizeClient.java @@ -18,6 +18,7 @@ import static org.apache.log4j.ConsoleAppender.SYSTEM_OUT; +import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.speech.v1beta1.RecognitionConfig; import com.google.cloud.speech.v1beta1.RecognitionConfig.AudioEncoding; import com.google.cloud.speech.v1beta1.SpeechGrpc; @@ -28,6 +29,8 @@ import com.google.protobuf.TextFormat; import io.grpc.ManagedChannel; +import io.grpc.ManagedChannelBuilder; +import io.grpc.auth.ClientAuthInterceptor; import io.grpc.stub.StreamObserver; import org.apache.commons.cli.CommandLine; @@ -47,6 +50,7 @@ import java.util.Arrays; import java.util.List; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; @@ -84,7 +88,7 @@ public StreamingRecognizeClient(ManagedChannel channel, String file, int samplin //Send log4j logs to Console //If you are going to run this on GCE, you might wish to integrate with gcloud-java logging. //See https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/README.md#stackdriver-logging-alpha - + ConsoleAppender appender = new ConsoleAppender(new SimpleLayout(), SYSTEM_OUT); logger.addAppender(appender); } @@ -93,6 +97,17 @@ public void shutdown() throws InterruptedException { channel.shutdown().awaitTermination(5, TimeUnit.SECONDS); } + static ManagedChannel createChannel(String host, int port) throws IOException { + GoogleCredentials creds = GoogleCredentials.getApplicationDefault(); + creds = creds.createScoped(OAUTH2_SCOPES); + ManagedChannel channel = + ManagedChannelBuilder.forAddress(host, port) + .intercept(new ClientAuthInterceptor(creds, Executors.newSingleThreadExecutor())) + .build(); + + return channel; + } + /** Send streaming recognize requests to server. */ public void recognize() throws InterruptedException, IOException { final CountDownLatch finishLatch = new CountDownLatch(1); @@ -242,7 +257,7 @@ public static void main(String[] args) throws Exception { System.exit(1); } - ManagedChannel channel = AsyncRecognizeClient.createChannel(host, port); + ManagedChannel channel = createChannel(host, port); StreamingRecognizeClient client = new StreamingRecognizeClient(channel, audioFile, sampling); try { client.recognize(); diff --git a/speech/grpc/src/main/java/com/examples/cloud/speech/SyncRecognizeClient.java b/speech/grpc/src/main/java/com/examples/cloud/speech/SyncRecognizeClient.java index cf6208774da..9b8e68efb17 100644 --- a/speech/grpc/src/main/java/com/examples/cloud/speech/SyncRecognizeClient.java +++ b/speech/grpc/src/main/java/com/examples/cloud/speech/SyncRecognizeClient.java @@ -16,6 +16,7 @@ package com.examples.cloud.speech; +import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.speech.v1beta1.RecognitionAudio; import com.google.cloud.speech.v1beta1.RecognitionConfig; import com.google.cloud.speech.v1beta1.RecognitionConfig.AudioEncoding; @@ -25,7 +26,9 @@ import com.google.protobuf.TextFormat; import io.grpc.ManagedChannel; +import io.grpc.ManagedChannelBuilder; import io.grpc.StatusRuntimeException; +import io.grpc.auth.ClientAuthInterceptor; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; @@ -38,6 +41,7 @@ import java.net.URI; import java.util.Arrays; import java.util.List; +import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; @@ -49,15 +53,15 @@ public class SyncRecognizeClient { private static final Logger logger = Logger.getLogger(SyncRecognizeClient.class.getName()); - private static final List OAUTH2_SCOPES = - Arrays.asList("https://www.googleapis.com/auth/cloud-platform"); - private final URI input; private final int samplingRate; private final ManagedChannel channel; private final SpeechGrpc.SpeechBlockingStub speechClient; + private static final List OAUTH2_SCOPES = + Arrays.asList("https://www.googleapis.com/auth/cloud-platform"); + /** * Construct client connecting to Cloud Speech server at {@code host:port}. */ @@ -78,6 +82,17 @@ public void shutdown() throws InterruptedException { channel.shutdown().awaitTermination(5, TimeUnit.SECONDS); } + static ManagedChannel createChannel(String host, int port) throws IOException { + GoogleCredentials creds = GoogleCredentials.getApplicationDefault(); + creds = creds.createScoped(OAUTH2_SCOPES); + ManagedChannel channel = + ManagedChannelBuilder.forAddress(host, port) + .intercept(new ClientAuthInterceptor(creds, Executors.newSingleThreadExecutor())) + .build(); + + return channel; + } + /** Send a non-streaming-recognize request to server. */ public void recognize() { RecognitionAudio audio; @@ -179,7 +194,7 @@ public static void main(String[] args) throws Exception { System.exit(1); } - ManagedChannel channel = AsyncRecognizeClient.createChannel(host, port); + ManagedChannel channel = createChannel(host, port); SyncRecognizeClient client = new SyncRecognizeClient(channel, URI.create(audioFile), sampling); try { client.recognize(); diff --git a/speech/grpc/src/test/java/com/examples/cloud/speech/StreamingRecognizeClientTest.java b/speech/grpc/src/test/java/com/examples/cloud/speech/StreamingRecognizeClientTest.java index c42e521ebff..773af36c707 100644 --- a/speech/grpc/src/test/java/com/examples/cloud/speech/StreamingRecognizeClientTest.java +++ b/speech/grpc/src/test/java/com/examples/cloud/speech/StreamingRecognizeClientTest.java @@ -64,9 +64,9 @@ public void test16KHzAudio() throws InterruptedException, IOException { String host = "speech.googleapis.com"; int port = 443; - ManagedChannel channel = AsyncRecognizeClient.createChannel(host, port); + ManagedChannel channel = StreamingRecognizeClient.createChannel(host, port); StreamingRecognizeClient client = new StreamingRecognizeClient(channel, path.toString(), 16000); - + client.recognize(); assertThat(writer.toString()).contains("transcript: \"how old is the Brooklyn Bridge\""); } @@ -78,7 +78,7 @@ public void test32KHzAudio() throws InterruptedException, IOException { String host = "speech.googleapis.com"; int port = 443; - ManagedChannel channel = AsyncRecognizeClient.createChannel(host, port); + ManagedChannel channel = StreamingRecognizeClient.createChannel(host, port); StreamingRecognizeClient client = new StreamingRecognizeClient(channel, path.toString(), 32000); client.recognize();