Authenticate a facebook user in a Firefox plug-in

2020-07-27 02:33发布

问题:

I'm trying to write a Firefox plug-in that accesses data from facebook. Now I'm not sure how to get an access token. I tried to implement the client side flow for desktop apps (with the fixed redirect uri), but the big problem I encounter there, is that JavaScript doesn't allow me to wait for the redirect to happen. Any idea how this could be done?

As far as I understood it, because I don't have a webpage, the JavaScript API doesn't help much, right?

回答1:

I guess that you are opening https://www.facebook.com/dialog/oauth in a browser tab to let the user log in and give you access. You don't need to pass a working redirect URL here, you can rather use something that will definitely not work, like http://my.extension.local/. Then you only need to detect when the tab gets redirected to that URL. If you have a classic extension, you register a progress listener on the <browser> element of that tab and look at onLocationChange() calls - once you see a location starting with http://my.extension.local/ you can cancel the request and close the tab, the necessary data is in the URL. If you use the Add-on SDK you can attach a ready event listener to the tab, something along these lines:

var tabs = require("tabs");
tabs.open({
  url: "https://www.facebook.com/dialog/oauth?...",
  inBackground: false,
  onReady: function(tab)
  {
    if (tab.url.indexOf("http://my.extension.local/") == 0)
    {
      ...
    }
  }
});