【问题标题】:How to pass dynamic Google Cloud API Key to client side from server?如何将动态 Google Cloud API 密钥从服务器传递到客户端?
【发布时间】:2020-10-26 07:22:45
【问题描述】:

目前,我正在测试 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 密钥。

【问题讨论】:

    标签: google-cloud-platform speech-recognition google-speech-to-text-api


    【解决方案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 密钥,用户可以很容易地拦截该密钥并在密钥有效期间发送无限(并发)请求。所以我认为我的解决方案更好。您甚至可以在云功能中记录用户请求的进程 ID 和处理时间。有了这些信息,您就可以计算/检索估计成本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    相关资源
    最近更新 更多