我在 Google Cloud 上的 Node.JS 中有一個云函式,我需要向 Google 發出 GET 請求并且它需要一個身份驗證令牌。使用curl你可以生成一個使用$(gcloud auth application-default print-access-token)。但這在 Cloud 實體中不起作用,所以我如何生成一個?
部分功能:
exports.postTestResultsToSlack = functions.testLab
.testMatrix()
.onComplete(async testMatrix => {
if (testMatrix.clientInfo.details['testType'] != 'regression') {
// Not regression tests
return null;
}
const { testMatrixId, outcomeSummary, resultStorage } = testMatrix;
const projectID = "project-feat1"
const executionID = resultStorage.toolResultsExecutionId
const historyID = resultStorage.toolResultsHistoryId
const historyRequest = await axios.get(`https://toolresults.googleapis.com/toolresults/v1beta3/projects/${projectID}/histories/${historyID}/executions/${executionID}/environments`, {
headers: {
'Authorization': `Bearer $(gcloud auth application-default print-access-token)`,
'X-Goog-User-Project': projectID
}
});
uj5u.com熱心網友回復:
在花費了無數個小時之后,我偶然發現了滾動自動完成建議的答案。Google 有關于身份驗證的檔案,但沒有提到 Cloud Functions 發出 API 請求所需的內容:
const {GoogleAuth} = require('google-auth-library');
const auth = new GoogleAuth();
const token = await auth.getAccessToken()
const historyRequest = await axios.get(
`https://toolresults.googleapis.com/toolresults/v1beta3/projects/${projectID}/histories/${historyID}/executions/${executionID}/environments`,
{
headers: {
'Authorization': `Bearer ${token}`,
'X-Goog-User-Project': projectID
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/361606.html
