问题描述
我尝试使用
如果此链接位于浏览器地址栏中,它将显示以下消息:
这意味着返回的授权响应无法在浏览器中显示并被翻译为有效的DOM。
如何解决此问题?
JSONP需要以下任何一项工作:
- 匿名访问,这不适用于我们呃访问webapp模式 - 你需要将它运行为像我一样执行。
- 一个不使用任何需要授权的服务的脚本,在这种情况下你可以选择user访问webapp。这仅适用于登录Google的用户,并且在以前的模式下使用它的唯一好处是,所用服务的任何配额都将应用于每个用户,而不是跨所有Web应用的用户。一般来说,优先考虑选项1.
- 已由用户预授权的脚本。这也只适用于登录用户,但它可以让你使用任何服务。请参阅下面的介绍如何使用它。
我假设您正在尝试使用3 - 否则使用1,它会工作。如果您确实需要3(例如,访问用户电子邮件的JSONP服务),您必须授予服务授权才能使用该服务。要做到这一点,请在doGet()中添加如下内容:
函数doGet(e){
if e.parameter.auth)
返回HtmlService.createHtmlOutput('谢谢!现在脚本可以运行!');
// ...其余代码
}
然后你可以为用户提供指向他们可访问的script.google.com/YOUR_SCRIPT_URL?auth=1的链接。 (值1不重要......任何值都可以)。他们将获得授权页面,如果他们接受授权,他们将看到您的感谢信息,JSONP服务将为他们工作。这只需要做一次......脚本会在用户下次访问您的页面时记住授权,除非您已将其更改为需要新权限或用户已撤销授权。
I'm trying to use ContentService, to serve JSONP in a webpage with Google Apps Script, following the samples provided in this link under "Serving JSONP in Web Pages" sub-topic.
GS code:
function doGet(request) {
var events = CalendarApp.getEvents(
new Date(Number(request.parameters.start) * 1000),
new Date(Number(request.parameters.end) * 1000));
var result = {
available: events.length == 0
};
return ContentService.createTextOutput(
request.parameters.prefix + '(' + JSON.stringify(result) + ')')
.setMimeType(ContentService.MimeType.JSON);
}
HTML/JS Code:
<script src="https://script.google.com/macros/s/AKfycbwZCY0qrZ09szGvKOttA30IaJkdMAZrh_oNnvv0qzCqFWyuO5Wc/exec?start=1325437200&end=1325439000&prefix=alert"></script>
I published it, by deploying the GS code as webapp
, with the following parameters:
- Executing the app as: User accessing the web app
- Who has access to the app: Anyone
Unfortunately, whenever I try to run/open the HTML file containing the code, I got the following error in browser's console, and nothing happens:
How to run this sample provided in Google Apps Script link without errors ?
[OPTIONAL] Investigating the issue in details:
The error found in resources files generate by the browser is as follows:
And the browser translated DOM is as follows:
<html>
<head>
<script src="https://script.google.com/macros/s/AKfycbwZCY0qrZ09szGvKOttA30IaJkdMAZrh_oNnvv0qzCqFWyuO5Wc/exec?start=1325437200&end=1325439000&prefix=alert">
</script>
</head>
<body></body>
</html>
The generated link, in my case is:
"https://script.google.com/macros/s/AKfycbwZCY0qrZ09szGvKOttA30IaJkdMAZrh_oNnvv0qzCqFWyuO5Wc/exec?start=1325437200&end=1325439000&prefix=alert"
If this link is placed in browser address bar, it shows the following message:
which means that the returned authorization response wasn't able to be displayed on browser and being translated as valid DOM.
How to overcome this issue ?
JSONP requires any one of the following to work:
- anonymous access, which isn't available with the "user accessing the webapp" mode - you need to run it as "execute as me".
- a script that doesn't use any services requiring authorization, in which case you can choose "user accessing the webapp." This will only work for users logged into Google, and the only advantage of using it over the previous mode is that any quota for services used will be applied per user rather than across all users of the webapp. In general, prefer option 1 over this.
- a script that has been pre-authorized by the user. This will also only work for logged-in users, but it will let you use any services. See below for how to use this.
I am assuming that you are trying to use 3 - otherwise, use 1 and it will just work. If you do need 3 (for example, a JSONP service that accesses the user's email) you will have to give them the ability to authorize the service before it will work. To do that, add something like this to your doGet():
function doGet(e) {
if (e.parameter.auth)
return HtmlService.createHtmlOutput('Thank you! Now the script can run!');
// ... rest of your code
}
Then you can give users a link to script.google.com/YOUR_SCRIPT_URL?auth=1 which they can visit. (The value 1 doesn't matter... any value will do). They will get the authorization page, and if they accept the authorization they will see your thank you message and the JSONP service will work for them. This only has to be done once... the script will remember the authorization the next time the user visits your page, unless you've changed it to require new permissions or the user has revoked the authorization.
这篇关于JSONP使用Google Apps脚本的ContentService?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!