huynqbibabo / react-native-google-cloud-speech-to-text

MIT License
17 stars 4 forks source link

TypeError: null is not an object (evaluating 'GoogleCloudSpeechToText.start') #1

Closed Siminn-teiturg closed 3 years ago

Siminn-teiturg commented 3 years ago

I am using the example from the readme. I receive this error when I press "Start me" on my debug Android device. I have verified that permissions are not the issue and that my Google API key works. I can't seem to find the problem.

Possible Unhandled Promise Rejection (id: 0):
TypeError: null is not an object (evaluating 'GoogleCloudSpeechToText.start')
Siminn-teiturg commented 3 years ago

Turns out the API key was the issue all along. I was unable to get it in google-services.json format. That would require integration with Firebase which had me running in circles and no results. To reiterate: a "google-services.json" file is only obtainable through a Firebase project, which has nothing whatsoever to do with Google Cloud Speech-to-Text. I fail to see how this has any relevance to this library nor why it is referred to in the readme.

I used the setApi() method. The private key is not enough. I had to stringify the entire JSON file from my Google Cloud S2P API service account. Setting the system variable GOOGLE_APPLICATION_CREDENTIALS does not work either.

Siminn-teiturg commented 3 years ago

I was wrong. setApiKey() is not working. Speech is never recognized in the client. The microphone seems to work and the app does not crash and I receive no errors from React. Logcat shows an invalid API key error. I do have a valid api key though. My api key works perfectly fine with the NodeJS example on the Google Cloud docs.

https://cloud.google.com/speech-to-text/docs/quickstart-client-libraries#client-libraries-usage-nodejs

I obtained an api key through my Google Cloud service account which has Speech-to-Text enabled. The key is of this form:

  "type": "",
  "project_id": "",
  "private_key_id": "",
  "private_key": "",
  "client_email": "",
  "client_id": "",
  "auth_uri": "",
  "token_uri": "",
  "auth_provider_x509_cert_url": "",
  "client_x509_cert_url": ""

What am I missing? How do I use setApiKey correctly? The most bizarre thing is that I am unable to reproduce my original error:

Possible Unhandled Promise Rejection (id: 0):
TypeError: null is not an object (evaluating 'GoogleCloudSpeechToText.start')

Even if I remove the stringified api key. I still get onVoice through console.log which suggests that the microphone is working. I am only able to see the api key error in Android Studio logcat.

E/SpeechService: Error calling the API.
    io.grpc.StatusRuntimeException: INVALID_ARGUMENT: API key not valid. Please pass a valid API key.

But as I've said I have verified that my API key is valid. This is bewildering to say the least.

Siminn-teiturg commented 3 years ago

I haven't solved the issue but I altered the library to use a local speech-to-text service which is based on the Google Cloud API and it works like a charm. This must mean my issue has to do with my Google Cloud key. I tried a service account credentials as well as an API & Services key, both of which have access to the enabled Google Speech-to-Text service to no effect. Regardless, the issue isn't to do with this library it seems.