Google apps script 如何在google应用程序脚本中包含aws cognito身份验证?
从GoogleSheet的应用程序脚本中,我们试图通过aws cognito身份验证来访问我们的数据库。我甚至在入门时遇到了麻烦,因为我不知道如何导入模块,因为这不是node.js环境。有没有人有这样做的经验并且不介意分享?Google apps script 如何在google应用程序脚本中包含aws cognito身份验证?,google-apps-script,amazon-cognito,Google Apps Script,Amazon Cognito,从GoogleSheet的应用程序脚本中,我们试图通过aws cognito身份验证来访问我们的数据库。我甚至在入门时遇到了麻烦,因为我不知道如何导入模块,因为这不是node.js环境。有没有人有这样做的经验并且不介意分享? 谢谢我就是这样做到的: var authEndpoint = "https://[put your auth domain here].amazoncognito.com/oauth2/token"; var authClientId = "[
谢谢我就是这样做到的:
var authEndpoint = "https://[put your auth domain here].amazoncognito.com/oauth2/token";
var authClientId = "[put your Cognito client ID here]";
var authSecret = "[put your Cognito auth secret string here]";
var authExpirationTime = undefined;
var authLatestToken = undefined;
function getAuthToken() {
// If last known token is still valid, use it.
if (authLatestToken && authExpirationTime && authExpirationTime < new Date().getTime()) {
return authLatestToken;
}
// Otherwise, request new token from AWS Cognito
var params = {
method: "POST",
followRedirects: true,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic " + Utilities.base64Encode(authClientId + ":" + authSecret)
},
payload: "grant_type=client_credentials"
};
var response = UrlFetchApp.fetch(authEndpoint, params);
// Verify response
if (response.getResponseCode() !== 200) {
throw new Error("Authentication failed: HTTP Response not OK.");
}
var data = JSON.parse(response.getContentText());
if (!data.access_token) {
throw new Error("Authentication failed: No token returned.");
}
// Set internal vars and return token
authLatestToken = data.access_token;
if (data.expires_in > 0) {
authExpirationTime = new Date().getTime() + data.expires_in * 1000;
}
return authLatestToken;
}
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + getAuthToken()
},