Google Speech API returns PERMISSION_DENIED the request cannot be identified with a client project

396 Views Asked by At

So i modified the Google Speech API Example code (didn't work before either) to be on par with their update notice (regarding the deprecation in ClientAuthenticator ) and get the same error as before with no resolution, with the host being speech.googleapis.com/v1beta1/speech:asyncrecognize?key=My_Key my code is as follows:

public static ManagedChannel createChannel(String host) throws IOException {
  FileInputStream filestream = new FileInputStream("/home/admin/creds.json");

  GoogleCredentials creds = GoogleCredentials.fromStream(filestream).createScoped(OAUTH2_SCOPES);

  ManagedChannel channel = ManagedChannelBuilder.forTarget(host).build();
  GreeterGrpc.GreeterStub stub = GreeterGrpc.newStub(channel);
  stub = stub.withCallCredentials(MoreCallCredentials.from(creds));
  System.out.println(channel.authority()+creds);

  filestream.close();

  return channel;
}

but it simply returns a

WARNING: RPC failed: Status{code=PERMISSION_DENIED, description=The     request cannot be identified with a client project. Please pass a valid API key with the request., cause=null}

any help or advice is welcome, Thanks!

1

There are 1 best solutions below

0
On

Hey instead of doing this,

GreeterGrpc.GreeterStub stub = GreeterGrpc.newStub(channel); stub = stub.withCallCredentials(MoreCallCredentials.from(creds));

please do following:

GreeterGrpc.GreeterStub stub = GreeterGrpc.newStub(channel).withCallCredentials(MoreCallCredentials.from(creds));

As your code reinitiate the stub object, you are getting PERMISSION_DENIED.

I hope it helps.