0

I am developing an android application, which uses Google Speech to text via the Google cloud streaming Speech API at the moment, however I am having trouble to get the authentication to work.

I want to authenticate at the Google Cloud API using OAuth2 and a authkey.json from a Google Service account.

I use the SpeechGrpc.SpeechStub class from package google.cloud.speech.v1beta1 to call RPC methods. This needs a ManagedChannel in the constructor through which it communicates with the API.

I found an example showing how to create a ManagedChannel with GoogleCrendential here: GoogleCloudPlatform/java-docs-samples

It worked until the point of intercepting the credentials into the channel. The ClientAuthInterceptor class, which is used in the example to intercept the credentials into the channel is

  1. deprecated
  2. takes only com.google.auth.Credentials as argument while GoogleCredential inherits from com.google.api.client.auth.oauth2.Credential.

Next try: google example: Using OAuth 2.0 with the Google API Client Library for Java

It advises me to use a Plus to wrap the GoogleCrendential. But the Plus is also deprecated and does not even contain either a method

builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential)

as shown in topic GoogleCredential on that page, nor a method

Plus.Builder(httpTransport, jsonFactory, credential)
    .setApplicationName(APPLICATION_NAME).build();

As shown in section ServiceAccounts on that page.

I am running out of options as the SpeechGrpc.SpeechStub class, which is used to call rpc methods, needs a ManagedChannel to work.

An other option could maybe be to add the credentials to the SpeechGrpc.SpeechStub and not to the ManagedChannel through the method

SpeechGrpc.newStub(channel).withCallCredentials(CallCredentials creds)

If this is possible, I would like to know, how I can create CallCredentials out of a Service Account's authkey.json file.

I read through every example, google documentation and thread on this topic I could find within the last weeks, but I don't get this authentication to work.

I hope anyone in here now can help me out of this confusing google api and library issue.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Any special reason you want to use a service account and oauth when this is a public api and you could just use an api key? – Linda Lawton - DaImTo Mar 03 '17 at 11:24
  • Well the api is public, yes but for streaming use case I pay with my account. In all documentation it is told that one can use the api only through oauth ( ). For me it doesn't matter which auth I use. Do u have a working solution with api key method? – Martin Pressl Mar 03 '17 at 18:55
  • Api key will identify your account. Oauth is for accessing private data. – Linda Lawton - DaImTo Mar 03 '17 at 19:23

1 Answers1

0

Hi you just need to add one line before your code starts. In the google-doc-sample case add it in main() method very first line.

GoogleCredentials.fromStream(new FileInputStream());

Also, refer this link. https://github.com/google/google-auth-library-java

After 0.6.0 release google moved their authentication under this:

google-auth-library-oauth2-http