【问题标题】:How to invoke "authenticated" functions on GCP cloud functions如何在 GCP 云函数上调用“已验证”函数
【发布时间】:2019-08-21 10:13:44
【问题描述】:

在 GCP 云功能上,如果我取消选中 “允许未经身份验证的调用”,我只能通过提供的访问令牌 gcloud auth print-access-token 命令访问 HTTP API,这是一个 JWT 令牌,我该如何通过postman获取类似的访问令牌,以便我的移动应用程序可以获得类似的令牌并能够调用云功能?如果,我应该在 GCP 上设置自己的 OAuth 服务器吗?

PS请参考这个问题here

【问题讨论】:

    标签: google-cloud-platform google-cloud-functions


    【解决方案1】:

    这应该可以解决问题:

    curl -i https://[REGION]-[PROJECT_ID].cloudfunctions.net/[FUNCTION_NAME] -H "Authorization: bearer $(gcloud auth print-identity-token)"
    

    我还建议查看Authenticating Developers, Functions, and End-users 文档,了解更多使用 Google Cloud Functions 进行身份验证的方法。

    【讨论】:

    • 还要检查文档;)
    • 对不起,我可能不清楚这个问题。我有一个用户通过 firebase 登录,每次他们登录时,我都会得到他们的 id_token,如何将我的 firebase 用户连接到 GCP 云函数有一个经过身份验证的用户,以便我可以调用 CF?@gu
    • @guillaumeblaquiere,我看到了你的 [stackoverflow.com/questions/58245338/…
    • @mj21 你能帮我解决这个问题吗?
    【解决方案2】:

    这就是您可以通过编程方式生成令牌的方式 -

    // const url = 'https://TARGET_URL';
    const {GoogleAuth} = require('google-auth-library');
    const auth = new GoogleAuth();
    
    async function request() {
      if (!targetAudience) {
        // Use the request URL hostname as the target audience for requests.
        const {URL} = require('url');
        targetAudience = new URL(url).origin;
      }
      console.info(`request ${url} with target audience ${targetAudience}`);
      const client = await auth.getIdTokenClient(targetAudience);
      const res = await client.request({url});
      console.info(res.data);
    }
    
    request().catch(err => {
      console.error(err.message);
      process.exitCode = 1;
    });
    
    

    生成令牌后,您可以在标头中将其作为 -

    Authorization: Bearer ${myToken}
    

    在此处阅读更多文档 - https://cloud.google.com/functions/docs/securing/authenticating#functions-bearer-token-example-nodejs

    【讨论】:

      猜你喜欢
      • 2021-09-12
      • 1970-01-01
      • 2021-10-25
      • 2021-02-14
      • 1970-01-01
      • 2020-12-28
      • 2020-07-10
      • 2023-03-19
      • 1970-01-01
      相关资源
      最近更新 更多