【问题标题】:getting url of child window when it is redirected, from parent window(facebook oAuth generation)从父窗口(facebook oAuth生成)重定向时获取子窗口的url
【发布时间】:2014-02-28 17:34:42
【问题描述】:

我正在从一个封闭的应用程序进行 facebook 集成,facebook 提供了一种通过点击 url 来获取身份验证代码的方法

https://www.facebook.com/dialog/oauth?client_id={app-id}&redirect_uri=https://www.facebook.com/connect/login_success.html

当我们点击这个 url 时,facebook 登录窗口出现在屏幕上,登录后 facebook 重定向到 url {facebook_domain}/connect/login_success.html?code=XXXXXX_=_

我需要在我的父窗口或 java 代码中的 url 中使用这个 code,以便我可以进一步处理它。

我在想的方法是在子窗口中打开上面的 facebook 链接,使用 window.openiframe 并在父窗口中获取子窗口的重定向 url,但我无法获得重定向的 url父窗口中的子窗口。

我想不出任何其他方法。对于 facebook oAuth 代码生成您可以查看 facebook 开发人员文档(手动构建登录流程)。

【问题讨论】:

    标签: javascript facebook facebook-graph-api


    【解决方案1】:

    您可以编写一个自定义打开窗口函数,如下所示,以跟踪父页面中所有打开的子窗口,并在获取子对象并获取其href 一段时间后启动超时事件以关闭子窗口。我还没有测试过,但应该让你知道在哪里查看。

    //assuming there is only one child window
    var childWindow;
    var windowsOpened = [];
    
    function openChildWindow(url,name,params){
       windowsOpened.push(window.open(url,name,params));
       triggerTimeout(windowsOpened[0]);
       return windowsOpened[0];
    }
    
    function triggerTimeout(win){
       setTimeout(function(){ childWindow = win;},10);
    }
    
    function getUrlOfChild(){
       if(childWindow){
         alert(childWindow.location);
         childWindow.close();
       }
    }
    

    确保在服务器上运行并测试它,而不是直接作为 file://test.html。否则会抛出js协议错误

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 2011-02-23
      • 2010-11-22
      • 1970-01-01
      相关资源
      最近更新 更多