我在同一個谷歌云函式專案中有 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 代碼錯誤。保持此權限激活聽起來不太好,因為該功能已暴露。我嘗試使用此鏈接和此鏈接解決,但是沒有成功。
編輯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?
uj5u.com熱心網友回復:
您必須執行服務到服務的身份驗證。您可以在 Cloud Run 頁面中找到一個很棒的教程(好的,您使用 Cloud Functions,但底層基礎架構相同,檔案更好)。
您還必須了解Functions 身份以及如何更改它們(或授予當前服務帳戶正確的權限)
uj5u.com熱心網友回復:
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);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/421392.html
標籤:
