我正在研究google oauth2请求代码。我需要点击一个按钮应用程序,并打开一个新的窗口,以启动谷歌oauth2流。我将重定向uri设置为参数。我编写了这个函数,但是在setInterval函数中出现了这个错误:
Blocked a frame with origin from accessing a cross-origin frame这是一项功能:
oauth2SignIn() {
// Google's OAuth 2.0 endpoint for requesting an access token
const oauth2Endpoint = 'https://accounts.google.com/o/oauth2/v2/auth';
// Create element to open OAuth 2.0 endpoint in new window.
let form = document.createElement('form');
form.setAttribute('method', 'GET'); // Send as a GET request.
form.setAttribute('target', 'previewWindow');
form.setAttribute('id', 'googleFormAuth');
form.setAttribute('action', oauth2Endpoint);
// Parameters to pass to OAuth 2.0 endpoint.
const params = {
client_id: environment.GAPI_CLIENT_ID,
redirect_uri: 'https://my-app/services/api/api/google/calendar/callback',
scope: 'https://www.googleapis.com/auth/calendar.readonly',
state: 'try_sample_request',
response_type: 'code',
access_type: 'offline',
prompt: 'consent',
};
// Add form parameters as hidden input values.
for (let p in params) {
let input = document.createElement('input');
input.setAttribute('type', 'hidden');
input.setAttribute('name', p);
input.setAttribute('value', params[p]);
form.appendChild(input);
}
// Add form to page and submit it to open the OAuth 2.0 endpoint.
document.documentElement.appendChild(form);
let newWin = window.open('', 'previewWindow', 'popup');
form.submit();
setInterval(()=> {
if(newWin.location.href.includes('my-app') {
newWin.close();
}
})
}我的重点是当url更改时关闭newWin。我还必须读取文档,因为有时我在dom中打印了一个错误,就像服务器的401错误一样。这个问题有可能解决吗?
发布于 2022-09-22 16:33:12
嗨,Andy88,我希望我不会太晚,
由于谷歌已经将其认证模式从谷歌身份转换为一次触摸,我和你处于类似的地位。我使用我的后端端点/auth/redirected/:google作为重定向URL来检索刷新令牌、访问令牌、id令牌.并需要将访问令牌发送到客户端。到目前一切尚好。当访问令牌在我的弹出式窗口接收到时,我没有得到数据。当我想从弹出窗口获取数据时,我得到了以下错误:Blocked a frame with origin from accessing a cross-origin frame angular。这显然是有其安全原因的。
然后我想出了一条出路,这是可行的。我希望这不是最坏的方式,如果是的话,让我知道一个更好的方式。
我从后端发送了一个res.redirect('https://hereismyURL/login?status=200&token='+JSON.stringify(access_token));
当弹出被重定向到这个链接时,我正在寻找params,并将其保存到本地存储中。虽然这个我的等待页面在本地存储变量上有一个设置间隔,如果这个变量被设置为window.close,则弹出窗口,读取令牌和所需的内容(如果身份验证有效,则->继续,如果->不执行错误处理)
希望这能帮上忙..。
https://stackoverflow.com/questions/73092640
复制相似问题