我是电子新手,我目前正在尝试实施一个需要回调URI的OAuth2.0 API。 Url回调需要有效的网址(multi threaded)。所以我尝试了这段代码,但是没有用。
// Your GitHub Applications Credentials
var options = {
client_id: 'your_client_id',
client_secret: 'your_client_secret',
scopes: ["user:email", "notifications"] // Scopes limit access for OAuth tokens.
};
app.on('ready', () => {
// Build the OAuth consent page URL
var authWindow = new BrowserWindow({ width: 800, height: 600, show: false, 'node-integration': false });
var githubUrl = 'https://github.com/login/oauth/authorize?';
var authUrl = githubUrl + 'client_id=' + options.client_id + '&scope=' + options.scopes;
authWindow.loadURL(authUrl);
authWindow.show();
function handleCallback (url) {
console.log(url);
}
// Handle the response from GitHub - See Update from 4/12/2015
authWindow.webContents.on('will-navigate', function (event, url) {
handleCallback(url);
});
authWindow.webContents.on('did-get-redirect-request', function (event, oldUrl, newUrl) {
handleCallback(newUrl);
});
// Reset the authWindow on close
authWindow.on('close', function() {
authWindow = null;
}, false);
});
另外,我使用了角度js路线,但也没有用。
所以我想知道是否有办法在电子应用中运行服务器以从URL(https://myserver.com/sucess)提供应用程序,如果是这样,这会影响应用程序在打包和分发时的行为,我的意思是该应用程序将从同一端口运行 ...任何建议都有助于我如何解决这个问题。谢谢
答案 0 :(得分:1)
上周我遇到了同样的问题,我需要将我的电子应用程序与使用OAuth协议形式的vkontakte api集成。你可以做什么: 1)你启动本地节点http服务器,可能在我做的单独过程中。 2)您通过oauth链接请求代码并将重定向uri设置为http://127.0.0.1:8000/,由于某种原因https://localhost对我不起作用。 3)在主进程中你等待来自服务器的代码的消息,在服务器上实现相应的逻辑(当你收到请求和代码时,通过process.send发送回带有代码的父消息) 4)您从主进程请求访问令牌,您不应该更改redirect_uri。您再次从服务器捕获响应。 5)你得到access_token,你杀了服务器...... 但是,当我完成所有这些时,我会阅读他们的文档直到最后,并且有人声称独立应用程序,例如我的桌面应用程序可以通过"隐式流程"更容易地接收令牌,并且您只能获得一个令牌呼叫。希望我的经验可以在你的问题上推断出来。祝好运!