首页 > 像新浪微博应用授权页面,自动刷新原页面是如何做到的?

像新浪微博应用授权页面,自动刷新原页面是如何做到的?

1.未登录情况下,点击微博账号登陆

2.弹出了一个新浪微博的应用授权新窗口

3.点击连接后,自动刷新了原来的SegmentFault页面

我的问题是,第三步那里,B页面(微博授权页)是怎么做到的刷新A页面(SF首页)呢?


微博的API应该是有redirect_url的参数的吧,所以用户点击授权之后就跳转的sf的主页,然后好了啊。


在由 Sina Weibo 授权之后, 会跳到转 http://.com/user/oauth/weibo?code=bcxxxxxxxxxxxxxxxxxxxxx 这个页面,
然后这个页面的代码如下:

html<html><head><script>
    if(window.opener && window.opener.oauthRegister){
        window.opener.oauthRegister({"id":2141630195,"name":"dds_feng","description":"","address":"\u5176\u4ed6","avatar":"http:\/\/tp4.sinaimg.cn\/2141630195\/180\/0\/1","gender":1,"homepage":"","url":"http:\/\/weibo.com\/2141630195","type":"weibo","platform":"\u65b0\u6d6a\u5fae\u535a","token":"s:32:\"2.00Vqxxx020xxx0GRZrG\";"});
        window.opener=null;
        window.open('', '_self');
        window.close();
    } else {
        window.location.href = '/';
    }
</script></head>
        <body>跳转中, 请稍后...</body></html>

因为是使用的window.open开的新窗口进行授权的, 所以可以通过 window.opener 来访问到之前的那个窗口.
然后又因为这两个窗口 都是属于同一域的(.com)所以它们之间是可以互相访问的,
window.opener.oauthRegister 的代码如下:

javascriptwindow.oauthRegister = function() {
   location.href = "/user/bind"
};

将父窗口跳到 /user/bind 这个地址, 然后这个地址, 会根据 这个Weibo账号是否已经绑定过 sg 的账号, 而做不同的事情.


【热门文章】
【热门文章】