Using google speech cloud api from android give error in GOOGLE_APPLICATION_CREDENTIALS

江枫思渺然 提交于 2020-02-25 23:30:56

问题


I am trying to build an app that use google speech cloud api in android kotlin

here is my code

launch {
             googleTextToSpeech = TextToSpeechClient.create()
             googleTextToSpeech?.let {
             viewModel.speakGoogle(googleTextToSpeech!!, totalMessage, player)
          }
       }


 fun speakGoogle(textToSpeech: com.google.cloud.texttospeech.v1.TextToSpeechClient, message: String, player: MediaPlayer) {

        val filePath = Environment.getExternalStorageDirectory().absolutePath + "/google_" + System.currentTimeMillis() + ".mp3"

        launch {

            var msg = android.text.Html.fromHtml(message).toString()
            msg = msg.replace("\"", "")


            var input = SynthesisInput.newBuilder().setText(msg).build()
            var voice = VoiceSelectionParams.newBuilder().setLanguageCode("en-US").setSsmlGender(SsmlVoiceGender.FEMALE).build()
            var audio = AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.MP3).build()

            var response = textToSpeech.synthesizeSpeech(input, voice, audio)

            response?.let {

                try {
                    val inputStream = response.audioContent.toByteArray()

                    File(filePath).outputStream().use { inputStream }

                    player.setDataSource(filePath)
                    player.prepare()
                    player.start()
                } catch (e: IOException) {
                    Log.i("AMIRA00000", e.toString())
                } catch (e: IllegalStateException) {
                    Log.i("AMIRA00000", e.toString())
                }
            }
        }
    }

I am also having my google-service.json file included in the app

but I am getting the following error

java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:134)
at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:119)
at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:91)
at com.google.api.gax.core.GoogleCredentialsProvider.getCredentials(GoogleCredentialsProvider.java:67)
at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:135)
at com.google.cloud.texttospeech.v1.stub.GrpcTextToSpeechStub.create(GrpcTextToSpeechStub.java:74)
at com.google.cloud.texttospeech.v1.stub.TextToSpeechStubSettings.createStub(TextToSpeechStubSettings.java:100)
at com.google.cloud.texttospeech.v1.TextToSpeechClient.<init>(TextToSpeechClient.java:128)
at com.google.cloud.texttospeech.v1.TextToSpeechClient.create(TextToSpeechClient.java:109)
at com.google.cloud.texttospeech.v1.TextToSpeechClient.create(TextToSpeechClient.java:101)
at com.sbs16.ensofia.view.main.MainFragment$setupBinding$3$$special$$inlined$let$lambda$2.invokeSuspend(MainFragment.kt:186)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:238)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)

I am also having another json file thathave private key and other credential settings but I do not know how to use it


回答1:


I'm not an Android developer, but my feeling is that you shouldn't call the TextToSpeech directly from your Android app. You should call it through a backend (On AppEngine for example, or on Firebase Functions).

This backend is authenticated by the TextToSpeech API, and your Android client is authenticated on your backend.

Thereby, you can control who use your app, and your TextToSpeech feature. In any case, never put the service account secret key file in your app, either anybody that download you app can steal the key and perform call on the service account behalf and you will pay the bill!



来源:https://stackoverflow.com/questions/60310714/using-google-speech-cloud-api-from-android-give-error-in-google-application-cred

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!