嵌入 iFrame 时 Google Apps 脚本不会加载

问题描述 投票:0回答:2

我正在尝试将我的 Google Apps 脚本 Web 应用程序嵌入到另一个域上的 iFrame 中,但该 Web 应用程序未加载,我只看到白屏。 webinspector 中也没有错误。

Web 应用程序发布时: 以 m 身份执行e 并且 访问权限具有给定域内的任何人。

根据this我实现了我的doGet方法,如下所示:

function doGet(e) {
 return HtmlService
 .createHtmlOutputFromFile('html/index')
 .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}

IFrame 看起来像这样:

<iframe src="https://script.google.com/a/my_domain/macros/s/ADjidojcojv/exec" title="test" width="558" height="300"></iframe>

当用户登录 Google 时,将显示 Web 应用程序。然而,当用户未登录时,灰色图像带有

account.google.com refused to connect

我认为原因是有一个重定向到谷歌登录不允许显示。此外,在这种情况下,还有另一个到 SAML SSO 应用程序的重定向。因此,当您正常登录 google 时,您将重定向到 SAML SSO 登录。

我有什么选择?

[编辑] 我发现有人有“完全相同的问题”和一个可能的解决方案。显然没有简单的方法可以做到这一点......

google-apps-script iframe web-applications same-origin-policy x-frame-options
2个回答
5
投票

可能的解决方案:

提前警告用户必须登录
  • 使用访问权限发布:“任何人,甚至匿名”
  • 如果不可能,可以使用
  • window.length
  • (可跨源访问的少数属性之一)来检测 iframe 页面是否已加载窗口,如果页面拒绝连接且大于 0,则该值将为 0在网络应用程序中,因为您的页面是 iframe 的。然后就可以将用户重定向到登录页面。
    
    
  • <!DOCTYPE html> <html> <body> body <iframe src="https://script.google.com/macros/s/[SCRIPT_ID]/exec" >Loading </iframe> <script> (() => { console.log("loading page"); const f = document.querySelector("iframe"); f.onload = (e) => { console.log("iframe onload"); const fw = f.contentWindow; if (fw.length > 0) console.info("logged in"); else { alert("Please Login!"); window.location = "https://accounts.google.com"; } }; })(); </script> </body> </html>
参考资料:

    同源政策

0
投票
这才有效

第一步:

需要在doGet函数中添加setsandbox和setxframeoptionmode

例如

函数 doGet() {

var h = HtmlService.createTemplateFromFile('html') h.logobase64string = logourl() 返回 h.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME).setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL) }

第二步:

将您的 Web 应用程序部署为以“ME”身份执行并且有权访问“任何人”

第三步现在使用 iframe

© www.soinside.com 2019 - 2024. All rights reserved.