gpt4 book ai didi

api - 关闭窗口,登录后重定向到旧页面[客户端程序]

转载 作者:行者123 更新时间:2023-12-03 06:02:56 26 4
gpt4 key购买 nike

我正在使用youtube api,因此使用Oauth 2.0进行客户端授权。

https://developers.google.com/youtube/2.0/developers_guide_protocol_oauth2#OAuth2_Client_Side_Web_Applications_Flow



当单击登录名时,使用google oauth 2.0打开新的授权窗口,并被重定向,它将在新窗口(用于授权)中打开重定向的网址,登录成功/失败后如何处理,我关闭登录窗口,并返回原始网站并附带 token 和结果。

如果您建议使用 session 变量来实现此目的,我认为采用客户端编程方法将无法实现。

我需要与此处类似的登录/授权方法:

http://gdata-samples.googlecode.com/svn/trunk/gdata/youtube_upload_cors.html I tried to decipher the code, but it dint work out.



我更喜欢采用客户端方法,因为我打算从事的工作完全是客户端驱动(JS)

谢谢

最佳答案

最好的方法可能是HTML5 postMessage。

但是,在OAuth 2.0入门(由O'Reilly出版)中,我有使用标准JavaScript和window.opener的可行方法。在最近的浏览器中可以正常工作:

我有一个名为oauth2callback.html的页面。此页面是您的redirect_uri。它从URL的#哈希片段中解析访问 token ,在window.opener中调用JS函数来设置访问 token ,然后关闭自身:

<html>
<body>
<script type="text/javascript">
var oauthParams = {};

// parse the query string
// from http://oauthssodemo.appspot.com/step/2
var params = {}, queryString = location.hash.substring(1),
regex = /([^&=]+)=([^&]*)/g, m;
while (m = regex.exec(queryString)) {
oauthParams[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}

window.opener.setOauthParams(oauthParams);
window.opener.callApi();
window.close();
</script>
</body>
</html>

父页面(通过在弹出窗口中打开OAuth流来启动整个过程)具有一个简单的JavaScript函数 setOauthParams,该函数接受参数并将它们存储在全局变量中:
function setOauthParams(oauthParamsPassed) {
this.oauthParams = oauthParamsPassed;
}

我还有其他代码可以启动OAuth流程,并使用jQuery调用API(在我的情况下为Google联系人),但这听起来并不是您需要的。

关于api - 关闭窗口,登录后重定向到旧页面[客户端程序],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10782503/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com