使用来自Android的Google语音云API在GOOGLE_APPLICATION_CREDENTIALS中给出错误 [英] Using google speech cloud api from android give error in GOOGLE_APPLICATION_CREDENTIALS

查看:584
本文介绍了使用来自Android的Google语音云API在GOOGLE_APPLICATION_CREDENTIALS中给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建在Android Kotlin中使用Google语音云api的应用程序

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

这是我的代码

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())
                }
            }
        }
    }

我的应用程序中还包含我的google-service.json文件

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

但是出现以下错误

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)

我还有另一个具有私钥和其他凭据设置的json文件,但我不知道如何使用它

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

推荐答案

我不是Android开发人员,但我的感觉是您不应该直接从Android应用程序调用TextToSpeech.您应该通过后端来调用它(例如,在AppEngine上或在Firebase Functions上).

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).

此后端通过TextToSpeech API进行身份验证,而您的Android客户端在您的后端进行身份验证.

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

因此,您可以控制谁使用您的应用程序以及TextToSpeech功能.无论如何,切勿将服务帐户秘密密钥文件放入您的应用程序中,任何下载您应用程序的人都可以窃取密钥并代表服务帐户执行呼叫,您将支付账单!

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!

这篇关于使用来自Android的Google语音云API在GOOGLE_APPLICATION_CREDENTIALS中给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆