向 Google Apps 脚本发送 POST 请求
Posted
技术标签:
【中文标题】向 Google Apps 脚本发送 POST 请求【英文标题】:Sending POST requests to a Google Apps Script 【发布时间】:2020-11-03 08:59:23 【问题描述】:这很有效。
我能够通过 Google Apps 脚本创建一个端点,允许最终用户向我(或其他联系人)发布消息,并且还可以向他们发布该消息的副本。
POST 请求的代码类似于:
function doPost(e)
var response;
try
response = sendContactEmail(e.postData.contents);
catch (error)
throw JSON.stringify(error, null, '\t');
return ContentService
.createTextOutput(JSON.stringify(response))
.setMimeType(ContentService.MimeType.JSON)
现在,当我尝试它时,我遇到了问题。当我从我的 Angular 站点尝试它时,服务代码如下:
@Injectable()
export class ContactService implements SenderService
constructor(private http: HttpClient)
send(message: EmailMessage): Observable<any>
return this.http.post<any>(
"https://script.google.com/macros/s/AKfycbyEuvROpXUEi4wTX4N06nqF6oHlwihVc9Ut6-OG04zPi5yuOCzn/exec",
JSON.stringify( data: message ),
headers:
"Access-Control-Allow-Origin": "*",
,
);
它不起作用,我面临类似的问题
Access to XMLHttpRequest at 'https://script.google.com/macros/s/AKfycbyEuvROpXUEi4wTX4N06nqF6oHlwihVc9Ut6-OG04zPi5yuOCzn/exec' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
在尝试调试此问题时,我打开 Postman,发出请求,却得到以下 html 响应:
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="//ssl.gstatic.com/docs/script/images/favicon.ico">
<title>Error</title>
<style type="text/css">
body
background-color: #fff;
margin: 0;
padding: 0;
.errorMessage
font-family: Arial, sans-serif;
font-size: 12pt;
font-weight: bold;
line-height: 150%;
padding-top: 25px;
</style>
</head>
<body style="margin:20px">
<div><img src="//ssl.gstatic.com/docs/script/images/logo.png"></div>
<div style="text-align:center;font-family:monospace;margin:50px auto 0;max-width:600px">Authorization is
required to perform that action.</div>
</body>
</html>
我不太了解使用 Google Apps 脚本作为后端。
我应该为这种类型的东西设置 OAuth 令牌吗?如果可以,如何设置? 否则,这个问题我该怎么办?
目前,除了我的旧版 Google Apps 脚本之外,没有任何后端。
【问题讨论】:
我提出了一个修改点作为答案。你能确认一下吗?在我的环境中,我可以确认脚本通过修改工作。但是在您的环境中,当它不起作用时,我为此道歉。 这能回答你的问题吗? XMLHttpRequest blocked by CORS policy when posting data to a Web App @TheMaster 在axios.post
的情况下,发现Content-Type
是不需要的。 Ref 但是在我的环境中,好像在angular的情况下,当不使用Content-Type
时,会出现与CORS相关的错误。
@Tanaike 正如那个答案中所说,我相信答案的本质是“避免预检”。不同的库可能会将不同的标头设置为默认值。但是只要将Content-type
设置为“text/plain”或任何其他使请求“简单请求”的mime 类型,它就应该避免cors 错误。这就是我在此处链接该答案的原因。
@TheMaster 感谢您的回复。我忘记了请求标头可能取决于每个库。在那种情况下,我可以理解需要修复内容类型。当我想到OP的情况时,我认为内容类型可能已经设置好了。这样,就需要使用内容类型。另外,我可以修改this。感谢您提供更多信息。
【参考方案1】:
在这种情况下,使用"Content-Type": "text/plain"
代替"Access-Control-Allow-Origin": "*"
怎么样?
发件人:
headers:
"Access-Control-Allow-Origin": "*",
,
收件人:
headers:
"Content-Type": "text/plain"
,
注意:
在这种情况下,它假定您的 Google Apps 脚本端的doPost(e)
工作正常。当您的 Google Apps 脚本出现错误时,可能无法删除该错误。请注意这一点。
参考资料:
Web Apps Taking advantage of Web Apps with Google Apps Script【讨论】:
很遗憾,它没有用。我在 Angular 应用程序和 Postman 上都确认了这一点... @Mike Warren 感谢您的回复。我带来的不便表示歉意。我想确认你的情况。您的 Google Apps 脚本没有错误。我的理解正确吗?当 Google Apps 脚本出现错误时,无法删除该错误。请注意这一点。 @Mike Warren 顺便说一下,来自您问题中的Authorization is required to perform that action.
,在这种情况下,请使用Execute the app as: Me
和Who has access to the app: Anyone, even anonymous
作为简单情况测试您的Web 应用程序脚本。当您确认您的 Google Apps 脚本没有错误时,请将 Web Apps 重新部署为新版本。这样,最新的脚本就会反映到 Web 应用程序中。请注意这一点。
@Mike Warren 当Who has access to the app:
是Anyone
和Only myself
和/或Execute the app as:
是User accessing the web app
时,访问令牌是访问Web 应用程序所必需的,并且在您的在这种情况下,Google Apps 脚本项目需要与每个用户共享。所以也请注意这一点。
好的,所以当我运行我留在代码中的测试函数时,我会看到授权屏幕。当我全部接受并继续时,我遇到以下错误:` 异常:您无权调用 Session.getActiveUser。所需权限:googleapis.com/auth/userinfo.email(第 1 行,文件“代码”)`我仔细检查并确保 Execute the app as: Me
和 Who has access to the app: Anyone, even anonymous
以上是关于向 Google Apps 脚本发送 POST 请求的主要内容,如果未能解决你的问题,请参考以下文章
在 Google Apps 脚本中从另一个电子表格编辑一个电子表格
Google Apps 脚本中的 POST 请求和 405 错误