首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从云函数调用(私有)云函数

从云函数调用(私有)云函数
EN

Stack Overflow用户
提问于 2022-02-21 19:40:46
回答 1查看 356关注 0票数 0

我一直得到403的测试代码如下。是只有我一个人,还是在同一个项目中调用一个函数过于复杂?我做了一些这里这里的研究。

我已经在这两个函数的默认服务帐户上设置了云函数调用程序。和allow internal traffic

所以我已经尝试了下面这两种密码。令牌被打印到第一个函数的日志中,那么为什么我仍然得到403呢?

Script1:

代码语言:javascript
复制
const axios = require("axios");
/**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */
exports.helloWorld = async (req, res) => {
  console.log(JSON.stringify(process.env));

  const sample_api_url = `https://someRegionAndSomeProject.cloudfunctions.net/sample-api`;
  const metadataServerURL =
    "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity?audience=";
  const tokenUrl = metadataServerURL + sample_api_url;

  // Fetch the token
  const tokenResponse = await axios(tokenUrl, {
    method: "GET",
    headers: {
      "Metadata-Flavor": "Google",
    },
  });

  const token = tokenResponse.data;

  console.log(token);

  const functionResponse = await axios(sample_api_url, {
    method: "GET",
    headers: {
      Authorization: `Bearer ${token}`,
      "Content-Type": "application/json",
    },
  });
  
  const data = functionResponse.data;
  console.log(data);
  res.status(200).json({ token, data });
};

Script2:

代码语言:javascript
复制
const {GoogleAuth} = require('google-auth-library');
/**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */
exports.helloWorld = async (req, res) => {

const url = 'https://someRegionAndSomeProject.cloudfunctions.net/sample-api';
const targetAudience = url;
const auth = new GoogleAuth();
const client = await auth.getIdTokenClient(targetAudience);
const response = await client.request({url});

res.status(200).json({data: response.data})

};
EN

回答 1

Stack Overflow用户

发布于 2022-02-21 23:43:24

基于你的帖子,我创造了一个对我有用的样本。我创建了两个GCP函数"func1“和"func2”。然后,我决定了如何从func2中调用func1,其中func2仅暴露于运行func1的服务帐户标识中。

func1的最终代码如下:

代码语言:javascript
复制
const func2Url = 'https://us-central1-XXX.cloudfunctions.net/func2';
const targetAudience = func2Url;
const {GoogleAuth} = require('google-auth-library');
const auth = new GoogleAuth();

async function request() {
  console.info(`request ${func2Url} with target audience ${targetAudience}`);
  const client = await auth.getIdTokenClient(targetAudience);
  const res = await client.request({url: func2Url});
  console.info(res.data);
  return res;
}


exports.func1 = async (req, res) => {
  let message = `Hello from func1`;
  try {
    let response = await request();
    res.status(200).send(`${message} + ${response.data}`);
  }
  catch(e) {
    console.log(e);
    res.status(500).send('failed');
  }
};

这里使用的主要菜谱是Google 这里中描述的。我还发现这个中篇很有用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71212127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档