拒绝显示框架,因为它在 android webview 中将 X-Frame-Options 设置为 'DENY

Refused to display a frame because it set X-Frame-Options to 'DENY in android webview

当我尝试在 webview 中显示 google 日历时,它显示了一些错误:

[INFO:CONSOLE(0)] "Refused to display 'https://accounts.google.com/ServiceLogin?service=cl&passive=1209600&continue=https://www.google.com/calendar/embed?src%3Detlwhk@gmail.com%26ctz%3DAsia/Hong_Kong&followup=https://www.google.com/calendar/embed?src%3Detlwhk@gmail.com%26ctz%3DAsia/Hong_Kong&btmpl=mobile&ltmpl=mobilex&scc=1' in a frame because it set 'X-Frame-Options' to 'DENY'.", source: about:blank (0)

这是 html 代码

<p><iframe style="border: 0;" src="https://www.google.com/calendar/embed?src=etlwhk%40gmail.com&amp;ctz=Asia/Hong_Kong&amp;output=embed" width="800" height="600" frameborder="0" scrolling="no"></iframe></p>

android 端是一些简单的 webview 代码

        StringBuilder sb = new StringBuilder();
        sb.append("<HTML><HEAD><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0'><style>img{display: inline; height: auto; max-width: 100%}iframe{width:100%}</style></HEAD><body>");
        sb.append(page.page_content_chi.toString());
        sb.append("</body></HTML>");
        webview.loadDataWithBaseURL("file:///android_asset/", sb.toString(), "text/html", "utf-8", null); 

如何修复错误?感谢您的帮助。

您需要制作日历 public。这就是正在发生的事情——因为您尝试显示的日历未 publicly 共享,Google 日历首先想知道您是谁,以便决定向您显示什么,所以它会将您带到 Google 登录页面。登录页面通过禁止在 iframe 中显示自身来保护自己免受点击劫持(这就是 'X-Frame-Options' is set to 'DENY' 的意思)。

如果您将日历设置为 public 可见,日历将只显示它,而不会先尝试让您登录。关于如何共享日历,请参见:https://support.google.com/calendar/answer/37083

您可以通过创建一个将日历嵌入到 iframe 中的简单测试页面,然后在 window Google Chrome,您没有登录 Google 服务。 Chrome 也将拒绝显示日历,直到您将其设为 public 共享。

如果您的日历不是 public,请确保您已登录并接受第三方 cookie。否则 google 会将您转到登录页面,这将导致错误,因为在 iframe 中 google 将拒绝访问。