简体   繁体   中英

How to use oauthorizer in Firefox addon

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!

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:

eg replace require("sha1") to require("./sha1")

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM