我是 Electron 新手,目前正在尝试实现需要回调 URI 的 OAuth2.0 API。 URL 回调需要有效的 URL (https://myserver.com/sucess)。所以我尝试了这个代码片段但不起作用。
// 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 路线,但也不起作用。
所以我想知道是否有办法在 Electron 应用程序中运行服务器以从 URL (https://localhost:3000) 为应用程序提供服务,如果是这样,这将如何影响应用程序在打包和分发时的行为,我的意思是应用程序是否将从同一个端口
...任何建议都将有助于我如何解决这个问题。谢谢你
最佳答案
上周我遇到了同样的问题,我需要将我的 Electron 应用程序与使用 OAuth 协议(protocol)形式的 vkontakte api 集成。你可以做什么:
1)您启动本地节点http服务器,可能像我一样在单独的过程中。
2) 您通过 oauth 链接请求代码并将重定向 uri 设置为 http://127.0.0.1:8000/ , 出于某种原因 https://localhost对我不起作用。
3)在主流程中,您等待来自服务器的带有代码的消息,在服务器上实现相应的逻辑(当您收到请求和其中的代码时,通过流程发送回带有代码的父消息)
4)你从主进程请求访问 token ,你不应该改变redirect_uri。您再次捕获来自服务器的响应。
5)你得到access_token,你杀死了服务器......
但是,当我完成所有这些操作时,我一直阅读他们的文档,并指出独立应用程序(例如我的桌面应用程序)可以通过“隐式流”以更简单的方式接收 token ,并且您只需一个电话即可获得 token 。希望我的经验可以推断您的问题。祝你好运!
关于oauth - 如何在electron中集成OAuth2.0登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44086537/