首页 > 解决方案 > 我应该如何在我的 React 应用程序中对 Google Cloud Function 进行身份验证?

问题描述

我有一个 React + GraphQL 应用程序,它可以访问 Google Cloud Function 来运行一些代码。目前我允许未经身份验证的访问,但我希望将其锁定。我没有看到如何在我的 React 应用程序中进行这样的身份验证:

fetch("https://REGION-PROJECT_ID.cloudfunctions.net/FUNCTION_NAME", {
     method: 'POST',
     headers: {
          'Content-Type': 'application/json',
          'Authorization': access_token //  <-------- this is what I want
     },
     body: JSON.stringify(something)
}) 

此示例在此线程中进行了说明:https ://github.com/googleapis/google-auth-library-nodejs/issues/775

我很想使用服务帐户进行身份验证,但我不想使用 Node 服务器。我是否被迫走 API 密钥路线?

这是用于验证云功能的文档:

https://cloud.google.com/functions/docs/securing/authenticating

谢谢!

标签: reactjsgoogle-cloud-platformgraphqlgoogle-cloud-functionsserverless

解决方案


我知道我的用例应该有更好的解决方案。API Gateway 是新的,所以在我寻找选项时它没有出现。感谢您的回复。

https://cloud.google.com/api-gateway/docs/quickstart


推荐阅读