2014-10-27 54 views
0

我正在使用GitHub API,并且希望将返回的access_token存储到node-webkit中的localStorage中。在node-webkit中处理OAuth回调

所以,问题是,如何从远程(HTTP)回调URL(如http://localhost:2222/github-callback?code=somecodehere)获取令牌?

回答

1

打开授权页面在新窗口中

var authWindow = gui.Window.open("https://github.com/login/oauth/authorize"); 

有一个听众在其loaded事件

authWindow.on('loaded', function() { 
    if(authWindow.location.href.substr(0, 34) === "http://your.domain/github-callback") { 
    // do what you want 
    } 
    // maybe some cleanup 
} 
+0

你有没有测试过了吗? 'authWindow.document.location.href.substr(0,34)'不起作用。 – 0x142857 2014-10-30 15:18:09

+0

这可行,但您需要使用窗口而不是文档:'authWindow.window.location.href' – Kus 2015-05-11 01:54:05

+0

@Kus其实两者都是按照规范(参见[MDN](https://developer.mozilla.org/zh-CN/) docs/Web/API/Location)),但在这个场景中,你的方式更加语义化,所以是的,我已经解决了答案。 – 2015-05-12 10:40:58