我在 Google Cloud 上的 Node.JS 中有一个云函数,我需要向 Google 发出 GET 请求,并且它需要一个身份验证令牌。使用curl
你可以使用生成一个$(gcloud auth application-default print-access-token)
。但这在云实例中不起作用,那么我如何生成一个呢?
部分功能:
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
}
});
经过无数个小时的努力,我在自动完成建议中滚动时偶然发现了答案。 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
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)