当我将 doPost(或 doGet)函数部署为 wep 应用程序时, 我可以选择“谁有权访问”选项:
当我选择“Anyone with MY-DOMAIN”时,如何告诉doPost函数访问用户在域内?
出于安全考虑,我想使用“我的域内的任何人”而不是“任何人”。
1。我在独立脚本中准备了一个
doPost
函数如下
function doPost(e) {
const json = JSON.parse(e.postData.contents);
const text = json.text;
return ContentService.createTextOutput(`The text you sent is "${text}"`);
}
3。我准备了另一个独立脚本来访问
doPost
函数,如下所示。
function myFunction() {
const app_url = "-----------Copied URL in the process #2.-------------";
const data = {'text': "Hogehoge" };
const params = {
'method': 'post',
'headers': { 'Authorization': 'Bearer ' + ScriptApp.getIdentityToken()},
"ContentType": "application/json",
'payload' : JSON.stringify(data),
'muteHttpExceptions': true
}
const resp = UrlFetchApp.fetch(app_url, params);
//Expected to output 'The text you sent is "Hogehoge"'
console.log(resp.getContentText());
}
我觉得request header应该包含一些关于account execute myFunction的信息,但是我不完全确定
ScriptApp.getIdentityToken()
是否合适。
为了获得 identityToken,我在 appscript.json 中添加了“https://www.googleapis.com/auth/userinfo.email”范围。
4。我使用域内的帐户执行
myFunction()
。返回的文本是:
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>