如何从node-webkit获取无托pipe页面的桌面应用程序的访问令牌?

我正在尝试使用node-webkit创build一个桌面应用程序。 这个应用程序的一部分需要使用Facebook获取/发布一些内容到任何个人资料(Facebook用户),使用我的个人电脑上的桌面应用程序。

关于facebook api文档( https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/v2.0 ),我必须手动实现loginstream程并使用以下URI作为redirectURI: https://www.facebook.com/connect/login_success.htmlhttps://www.facebook.com/connect/login_success.html

目前,在我的node-webkit应用程序中,通过一个子窗口(一个popup窗口),用户可以loginFacebook并授权我的桌面应用程序与其configuration文件进行交互。 这是代码的一部分:

 var url = "https://www.facebook.com/dialog/oauth?client_id=myclientID&redirect_uri=https://www.facebook.com/connect/login_success.html&response_type=token&scope=publish_actions"; loginWindow = window.open(url, 'Login facebook', 'location=yes,toolbar=yes,menubar=yes,directories=yes,status=yes,resizable=yes,scrollbars=yes,height=480,width=640', false); 

之后,用户被redirect到下面的URI,正如文档中提到的,访问令牌显示正确:

https://www.facebook.com/connect/login_success.html#access_token=theBigTokenString&expires_in=3864

但它只出现几秒钟,之后,该url被replace为https://www.facebook.com/connect/blank.html#_=_ (带有安全警告消息)。

我读过一些build议添加eventListener像hashchange到打开的窗口,以捕获访问令牌。 但是,在子窗口内部redirect后,我不再可以通过JavaScript与它进行交互。

所以最后,我无法获得子窗口已经检索的访问令牌,并使其可见几秒钟。

任何人都可以帮助我得到这个用户访问令牌node-webkit? 我真的不想在我的桌面应用程序和Facebook之间使用服务器(用于托pipe网页)。

预先感谢您的帮助。

在另一个问题的帮助下( 这里 ),我find了解决我的问题的方法。

我发布了我现在使用的代码,希望它能帮助别人:

 var url = "https://www.facebook.com/dialog/oauth?&client_id=myBigClientID&redirect_uri=https://www.facebook.com/connect/login_success.html&response_type=token&scope=publish_actions"; function Response() { this.access_token = null; this.expires_in = null; }; var response = new Response(); //function called every 500ms to check if token is in url window.hashUpdate = function() { if(window.loginWindow.closed){ window.clearInterval(intervalId); start(); //just a callback that I'm using to start another part of my application (after I caught the token) } else { var url = window.loginWindow.document.URL; var tabUrl = url.split("#"); //first split to separate the domain part and the parameter part var paramString = tabUrl[1]; //I concerned only by the second part after the '#' if(paramString != undefined){ var allParam = paramString.split("&"); for (var i = 0; i < allParam.length; i++) { var oneParamKeyValue = allParam[i].split("="); response[oneParamKeyValue[0]] = oneParamKeyValue[1]; //store my token in form of key => value }; //close the window after 1500ms setTimeout(function(){ window.loginWindow.close(); }, 1500); } } } //open the url and start the watching process window.loginWindow = window.open(this.url, 'Login facebook', false); this.intervalId = window.setInterval("window.hashUpdate()", 500);