0

目前,我正在测试 Google Cloud 的 Speech API 并想知道如何将动态 Google Cloud API 密钥从服务器传递给客户端应用程序。

语音功能将在客户端的应用程序(React Native)上。在对谷歌云 API 或会话的每个请求之前,我正在考虑从服务器端(Nodejs)动态生成 API 密钥,生命周期很短,然后传递到客户端。只有这样,客户才能使用 Google 服务。

主要担心的是我不想在客户端应用程序中嵌入 Google Cloud API 密钥,并且我想控制哪些客户端可以/不能使用该服务。有没有办法在服务器端动态生成 API 密钥并传递给客户端?谢谢。

更新:

我正在检查https://github.com/GoogleCloudPlatform/android-docs-samples/tree/master/speech/Speech并找到了建议:

This Android app uses JSON credential file locally stored in the resources. You should not do this in your production app. Instead, you should set up your own backend server that authenticates app users. The server should delegate API calls from your client app. This way, you can enforce usage quota per user. Alternatively, you should get the access token on the server side, and supply client app with it. The access token will expire in a short while.

这正是我想要做的,但任何人都可以建议我怎样才能做到这一点?谢谢。

在此处输入图像描述

我正在尝试找出如何在服务器后端获取第 2 步的 API 密钥。

4

1 回答 1

1

永远不要在用户的浏览器中存储凭据,幸运的是您正在努力遵守这一原则!

此处可能有用的设置具有以下组件:

  • 通过身份提供者在您的客户端应用程序中使用身份验证机制。您可以使用 Okta、Auth0、Cognito 或任何其他支持 Oauth2 的身份验证提供程序。
  • 使用相同的身份验证提供程序来保护自定义构建端点,它可以是 Google Cloud 函数。这可以与 Cloud Endpoints 结合使用,但不一定。
  • 在同一个 Cloud Function 中,检查用户身份后,调用语音 API。
  • API 密钥可以作为机密存储在 Google Secret Manager 中。
  • Cloud Function 充当 API 的“服务舱口”,将来自用户的请求来回传递到 Google Cloud Speech API。

您的 API 密钥保留在后端,作为机密存储。未使用身份验证提供程序进行身份验证的用户将永远无法访问语音 API。

云语音API

于 2020-10-26T10:15:44.040 回答