How to use oauthorizer in Firefox addon

1

I am trying to use oauth2 in Firefox, and I found this module from Google:

https://github.com/mozilla/oauthorizer

However, I am not sure how to use it.

I tried the following according to README of the project,

Components.utils.import("resource://oauthorizer/modules/oauthconsumer.js");

Yet it reports error of following:

"`Components` is not available in this context.Functionality provided by Components may be available in an SDKmodule: https://developer.mozilla.org/en-US/Add-ons/SDK However, if you still need to import Components, you may use the`chrome` module's properties for shortcuts to Component properties:Shortcuts: Cc = Components.classes Ci = Components.interfaces Cu = Components.utils CC = Components.Constructor Example: let { Cc, Ci } = require('chrome');"

Then, I tried this:

let {Cu} = require("chrome");
Cu.import("resource://oauthorizer/modules/oauthconsumer.js");

But I got this error then:

"Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXPCComponents_Utils.import]"

May be I need to set up something in the package.json? Or there are some special mechanisms to import the external modules?

Any help is much appreciated!

javascript
oauth-2.0
firefox-addon
asked on Stack Overflow Aug 23, 2015 by Walty Yeung

1 Answer

1

Finally, this is what I did: (Please note that I used jpm for the addon development)

  1. Install oauthorizer using npm. Inside the root folder of addon, run the following:

npm install oauthorizer --save

  1. In the addon script (index.js), add the following:
var OAuthConsumer = require("oauthorizer/lib/oauthconsumer.js").OAuthConsumer;
  1. Modify the oauthconsumer.js a little bit to update the path:

e.g. replace require("sha1") to require("./sha1")

I am pretty sure it's not the best solution, but at least it works.

answered on Stack Overflow Aug 23, 2015 by Walty Yeung

User contributions licensed under CC BY-SA 3.0