简体   繁体   English

在反应应用中将Google日历作为iframe嵌入?

[英]Embedding Google calendar as iframe in a react app?

Is there a way to implement an iframe of google calendar inside a react app without needing to manually handle events myself? 有没有办法在反应应用程序中实现谷歌日历的iframe,而无需自己手动处理事件?

Like the webapp would show the iframe where they can login to google calendar then it will just show it inside the specific box. 就像webapp会显示iframe,他们可以登录谷歌日历,然后它将只显示在特定的框内。

I can only find solutions using a calendar library but not the approach that I'm thinking about. 我只能使用日历库找到解决方案,但不能找到我正在考虑的方法。

Google has set the "X-Frame-Options" response header for requests to https://calendar.google.com/calendar/r (the default calendar homepage) to sameorigin , which means that this will not work: 谷歌已经为请求“X-框架-选项”响应头为https://calendar.google.com/calendar/r (默认日历主页),以sameorigin ,这意味着这行不通的:

<h2>Your Calendar:</h2>
<iframe src="https://calendar.google.com/calendar/r"></iframe>

If you try that, you will get a Refused to display '<URL>' in a frame because it set 'X-Frame-Options' to 'sameorigin'. 如果您尝试这样做,您将Refused to display '<URL>' in a frame because it set 'X-Frame-Options' to 'sameorigin'. . Essentially, the only domain that would be allowed to embed the default calendar pages would be Google's own domain. 从本质上讲,唯一允许嵌入默认日历页面的域名是Google自己的域名。

That being said, there is some interesting options available. 话虽如此,有一些有趣的选择。 If you know the users gmail address, even if their calendar is private, as long as they are logged in, they can see an iframe with a source equal to 'https://calendar.google.com/calendar/embed?src=' + encodeURI(email); 如果您知道用户的Gmail地址,即使他们的日历是私有的,只要他们已登录,他们就可以看到iframe等于'https://calendar.google.com/calendar/embed?src=' + encodeURI(email); .

Demo: JSfiddle link (the built in embed tool is giving me grief). 演示: JSfiddle链接 (内置嵌入工具让我感到悲伤)。

However, if they are not logged in, detecting that and showing the Google Login page, like you are asking for, is difficult. 但是,如果他们没有登录,则很难检测到并显示Google登录页面,就像您要求的那样。 If they are not logged in, an iframe with a GCal embed URL will automatically redirect to the login page, but because the login page has x-frame-options set to sameorigin , it will get blocked loading into the iframe. 如果他们没有登录,带有GCal嵌入URL的iframe将自动重定向到登录页面,但由于登录页面的x-frame-options设置为sameorigin ,因此将阻止加载到iframe中。 To see this in action, try the demo above in a logged-out incognito window and enter your gmail email. 要查看此操作,请在已注销的隐身窗口中尝试上面的演示,然后输入您的Gmail电子邮件。

Furthermore, as far as I can tell, you can't detect whether the loading of an iframe was blocked by x-frame-options with Javascript, so there is no easy way to redirect the user to login to Google if the calendar does not load. 此外,据我所知,您无法检测iframe的加载是否被Javascript的x-frame-options阻止,因此如果日历不是,则没有简单的方法可以将用户重定向到Google登录加载。

I have a feeling the most reliable way to check logged in status is going to be through the Google Sign-in button . 我有一种感觉,检查登录状态的最可靠方法是通过Google登录按钮 In addition, Google has a full API just for their Calendar product , including endpoints that would tell you which calendars a user has . 此外,Google还为其日历产品提供了完整的API ,包括可告诉您用户具有哪些日历的端点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM