在没有allUsers权限的情况下通过同一项目中的另一个调用云功能

Call cloud function through another in the same project without allUsers permission

我在同一个 google 云函数项目中有 2 个函数(myfunction1 和 myfunction2。

exports.myfunction1 = async (req, res) => {

 await axios({
                    method: 'post',
                        url: 'https://SERVER-PROJECT-ID.cloudfunctions.net/myfunction2',
                        timeout: 15000,
                        headers: {
                                    'Content-Type': 'application/json',
                                },
                                data: myjson
                        }).then(response => {
                           console.log(JSON.stringify(response.data));
                        }).catch(err => {
                            console.error("catch error");
                            console.error(err);
                     })
            }

它工作正常,但前提是我为 allUsers 配置了调用者权限。如果我删除此权限,则会收到 403 代码错误。保持此权限激活听起来不太好,因为该功能已公开。我尝试用这个 and this link 解决,但是没有成功。

编辑1:

const {GoogleAuth} = require('google-auth-library');
const auth = new GoogleAuth();

const targetAudience = 'https://SERVER-PROJECT-ID.cloudfunctions.net/myfunction2'

const url = '??????????';

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

我正在尝试使用这段代码,但是,const url 是谁?

您必须执行 service to service authentication。您可以在 Cloud 运行 页面中找到很棒的教程(您使用的是 Cloud Functions,但底层基础架构相同,文档更好)。

您还必须了解 Functions identity 以及如何更改它们(或授予当前服务帐户正确的权限)

let audience = 'https://SERVER-PROJECT-ID.cloudfunctions.net/myfunction2';
let token_request_url = 'http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity?audience=' + audience;

var token_response = await axios.get(token_request_url, { headers: {'Metadata-Flavor': 'Google'} });

let token_auth = token_response.data;

axios({
      method: 'post',
      url: audience,
        timeout: 15000,
          headers: {
                'Authorization': "Bearer " + token_auth
              },
                data: myJSON
    }).catch(err => {
        console.error(err);
});