Firebase身份验证有效,但使用Google Text to Speech API时出现错误
我已经建立了一个小型的android和firebase应用程序...身份验证就像一个超级按钮,在firebase控制台中,我可以看到使用Google帐户登录的用户.
I've set up a small android and firebase app... Authentification works like a charm, and in the firebase console, I can see my user, logged in with the Google account.
现在,我正在尝试使用文字转语音" api进行一些实验,并按照此教程进行操作:
Now I am trying to experiment a little with the Text to Speech api, and in doing so, I followed this tutorial:
https://github.com/GoogleCloudPlatform /java-docs-samples/tree/master/texttospeech/cloud-client
通过设置GOOGLE_APPLICATION_CREDENTIALS环境变量,我设法使小型Java应用程序正常工作(我按照本教程的步骤进行操作:
I managed to make the small java app work, by setting the GOOGLE_APPLICATION_CREDENTIALS Environment variable (I followed this tutorial for this step: https://cloud.google.com/docs/authentication/getting-started), but I am not sure what I need to do to make that code work in the Android app where the users are authentificated..
尝试调用TextToSpeech API时遇到的错误是:
The Error that I get when trying to make a call to the TextToSpeech API is:
应用程序默认凭据不可用.他们是 如果在Google Compute Engine中运行,则可用.否则, 必须定义环境变量GOOGLE_APPLICATION_CREDENTIALS 指向定义凭据的文件.看 https://developers.google.com/accounts/docs/application-default-凭据 有关更多信息.
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.
提到的错误来自以下行:
The error mentioned comes from the line:
TextToSpeechClient textToSpeechClient = TextToSpeechClient.create();
出现此错误的原因是,在android模拟器上,我无权访问在操作系统中设置为环境变量的凭据.因此,我必须以另一种方式提供凭据.
This error appears because of the fact that on the android emulator I don't have access to the credentials that are set as environment variable in my OS..So I have to provide the credentials in another way.
对于其他Google API(例如存储),我发现可以这样做:
In the case of other Google APIs, like Storage, I found out that this can be done like this:
// You can specify a credential file by providing a path to GoogleCredentials.
// Otherwise credentials are read from the GOOGLE_APPLICATION_CREDENTIALS environment variable.
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
.createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
我设法用json文件的内容创建了GoogleCredentials对象,但是TextToSpeech客户端似乎没有提供类似于以下功能:
I managed to create the GoogleCredentials object with the contents of the json file, however the TextToSpeech client doesn't seem to provide a functionality similar to this:
StorageOptions.newBuilder().setCredentials(credentials).build()
所以我的问题是....是否可以将凭据对象提供给TextToSpeech客户端?
So my question is....is there a way to provide the Credentials object to the TextToSpeech client?
谢谢
Currently, there is not a way to provide credentials to the TTS Client from this page.
出于安全性/身份验证的原因,我认为建议的最佳方法是使用 Firebase函数.
Due to Security / Auth reasons, I believe the best suggested approach is to use Firebase Functions.
- 获取文字
- 调用Firebase函数
- 具有Firebase函数调用TTS API
- 返回结果.
这样,应用程序内部不会泄漏任何密钥,您可以使用Firebase Auth.
This way, no keys are leaked inside the application and you can use Firebase Auth.
让我知道是否有帮助!
更新:
选项2: iOS教程(应适用于Android )
Option 2: iOS Tutorial (should be adaptable to Android)
- 获取文字
- 调用Firebase函数
- 具有Firebase函数返回OAuth2令牌
- 直接通过API使用令牌