我有一个使用Google javascript API的HTML5应用。我最近发现,该应用需要在iframe沙盒环境中运行。我知道,这看起来像是在解决沙盒环境的目标。但是,iframe仍然允许使用某些功能(请参阅下面的约束),这使我认为存在一些希望。
我在身份验证过程中遇到了问题:加载时,google javascript api在原始页面上添加了一个iFrame(在我的情况下已经是一个iframe),并使用postMessage机制在窗口之间进行通信。我想将Google api用于OAuth2.0身份验证过程以及查询API。
这是一个复制案例;我使用的是google提供的authSample.html页面的简化版本。
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>original auth page</title>
</head>
<body>
<button id="authorize-button">Authorize</button>
<div id="content">Not authentified</div>
<script type="text/javascript">
var clientId = '<use yours>';
var apiKey = '<use yours>';
var scopes = 'https://www.googleapis.com/auth/analytics.readonly';
var authorizeButton = document.getElementById('authorize-button');
var resultDiv = document.getElementById('content');
function handleClientLoad() {
gapi.client.setApiKey(apiKey);
authorizeButton.onclick = handleAuthClick;
}
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
makeApiCall();
} else {
resultDiv.innerHTML = "handleAuthResult Failed";
}
}
function handleAuthClick(event) {
gapi.auth.authorize({ client_id: clientId, scope: scopes, immediate: false }, handleAuthResult);
return false;
}
function makeApiCall() {
gapi.client.load('analytics', 'v3', function () {
resultDiv.innerHTML = "api call successful";
});
}
</script>
<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
</body>
</html>
最终用户将使用的包含页面如下所示:
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>iframed page</title>
</head>
<body>
<iframe id="8D681BA3DED5" width="400px" height="400px"
frameborder="0" sandbox="allow-scripts allow-forms allow-same-origin ms-allow-popups allow-popups" marginheight="0" marginwidth="0"
name="MyTest" src="https://localhost:4000/AuthSample.html"></iframe>
</body>
</html>
显然,在用服务器authSample.html网址替换iframe的实际src之后,您将必须执行此页面。
如果在不同的浏览器上运行它,您将注意到该失败的不同行为。我试图利用允许弹出窗口的事实,但似乎浏览器以不同的方式实现了打开的窗口也应该在沙盒环境中打开的事实。
我想避免编写自己的Web服务进行身份验证。另外,我相信用户可以使用通常的google网页(带有url accounts.google.com)输入凭据,从而更加安全。如果您有想法,可以在最近的浏览器中使用,请随时提交。
谢谢
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句