Firebase 身份验证有效,但在使用 Google Text to Speech API 时出现错误

Firebase authentification works but I get error when using the Google Text to Speech API

我设置了一个小型 android 和 firebase 应用程序...身份验证非常有效,在 firebase 控制台中,我可以看到我的用户,使用 Google 登录帐号。

现在我正在尝试使用 Text to Speech api 进行一些实验,在这样做的过程中,我遵循了这个教程:

https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/texttospeech/cloud-client

我设法通过设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量使小型 java 应用程序正常工作(我按照本教程完成此步骤:https://cloud.google.com/docs/authentication/getting-started),但我不确定我需要做什么才能使该代码在用户经过身份验证的 Android 应用程序中工作..

我在尝试调用 TextToSpeech API 时遇到的错误是:

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.

提到的错误来自行:

TextToSpeechClient textToSpeechClient = TextToSpeechClient.create();

出现此错误是因为在 android 模拟器上我无法访问在 OS 中设置为环境变量的凭据。所以我必须提供以另一种方式提供凭据。

在其他 Google API 的情况下,比如存储,我发现可以这样做:

// 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 客户端似乎没有提供与此类似的功能:

StorageOptions.newBuilder().setCredentials(credentials).build()

所以我的问题是....有没有办法向 TextToSpeech 客户端提供 Credentials 对象?

谢谢

目前,无法从 this page 向 TTS 客户端提供凭据。

由于安全/授权原因,我认为最好的建议方法是使用 Firebase Functions.

  1. 获取文本
  2. 调用 Firebase 函数
  3. 让 Firebase 函数调用 TTS API
  4. Return 结果。

这样,应用程序内部就不会泄露任何密钥,您可以使用 Firebase Auth。

如果有帮助请告诉我!

更新:

选项2:iOS Tutorial(应该适应Android)

  1. 获取文本
  2. 调用 Firebase 函数
  3. 具有 Firebase 功能return OAuth2 令牌
  4. 直接使用令牌 API