我应该如何在我的 React 应用程序中验证 Google Cloud Functions?

How should I approach authenticating a Google Cloud Function in my React application?

我有一个 React + GraphQL 应用程序,它连接到 Google 云函数以 运行 一些代码。目前我允许未经身份验证的访问,但我希望将其锁定。我没有看到如何在我的 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

我很想使用服务帐户进行身份验证,但我不想必须使用节点服务器。我是否被迫走 API 关键路线?

这是验证云函数的文档:

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

谢谢!

不建议从 Web 应用程序使用服务帐户身份验证。但是如果您出于测试目的需要在本地 运行 这个应用程序,我会说您可以使用 google-auth-library。可以找到更多信息和代码示例 here.

您还需要您正在使用的服务帐户的“Cloud Functions Invoker”角色。

话虽如此,我还是建议您将身份验证工作交给专门的身份验证提供程序,例如 Firebase 身份验证、Auth0 或 Okta。

我知道我的 use-case 应该有更好的解决方案。 API 网关是新的,所以当我寻找选项时它没有出现。感谢您的回复。

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