如何检测从Electron桌面应用程序打开的外部URI的重定向

时间:2017-06-16 17:10:04

标签: reactjs electron facebook-login desktop-application

我正在使用手动Facebook登录(https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow#dialogresponse)构建反应/电子桌面应用程序。

单击按钮,我使用shell.openExternal方法打开登录对话框,但我需要检测此重定向,然后从URI中读取访问令牌。我不知道该怎么做(在这里做出反应)。

哪种方法最好?

提前致谢。

1 个答案:

答案 0 :(得分:0)

正确的答案是遵循本教程:https://competenepal.com/lets-make-a-facebook-login-system-in-electron-that-actually-works/

我只需要实现这一部分:

var options = {
  client_id: '',
  scopes: "public_profile",
  redirect_uri: "https://www.facebook.com/connect/login_success.html"
};
var authWindow = new BrowserWindow({ width: 450, height: 300, show: false,
  parent: mainWindow, modal: true, webPreferences: {nodeIntegration:false} });
var facebookAuthURL = "https://www.facebook.com/v2.8/dialog/oauth?client_id=" + options.client_id + "&redirect_uri=" + options.redirect_uri + "&response_type=token,granted_scopes&scope=" + options.scopes + "&display=popup";
authWindow.loadURL(facebookAuthURL);
authWindow.show();
authWindow.webContents.on('did-get-redirect-request', function (event, oldUrl, newUrl) {
  var raw_code = /access_token=([^&]*)/.exec(newUrl) || null;
  var access_token = (raw_code && raw_code.length > 1) ? raw_code[1] : null;
  var error = /\?error=(.+)$/.exec(newUrl);

  if(access_token) {
    FB.setAccessToken(access_token);
    FB.api('/me', { fields: ['id', 'name', 'picture.width(800).height(800)'] }, function (res) {
      console.log('response is:', res);
    });
    authWindow.close();
  }
});