Use OAuth 2.0 in Firefox WebExtension

(I have asked the same question in stackoverflow, and unfortunately I got no reply on this. And an AMO editor introduced this forum to me.)

I have built an open sourced extension some time ago, and the extension needs OAuth 2.0 to access some Google Drive API.

Currently, my Chrome extension uses identity.launchWebAuthFlow to access to the OAuth, and Firefox XPI extension uses oauthorizer for the OAuth.

Now, I want to convert the Firefox extension from XPI to WebExtension for easier maintenance in the future (as Chrome and Firefox WebExtensions are very much alike).

I thought the migration should be easy as I already got two versions of extensions on hand. But after some research I can’t find an easy way to access the OAuth 2.0 inside the Firefox WebExtension.

Some advices would be greatly appreciated.

Thanks a lot!

I think the identity API is currently being implemented, didn’t find the bug, but I know there is one. Meanwhile you can kinda emulate it with webNavigation and storage.local, though I’m not 100% positive if storage.local is really the solution. See for example https://github.com/freaktechnik/advanced-github-notifier/blob/master/scripts/background.js#L190-L251

hi martin,

thanks a lot for your reply.

In this case may be I should just wait for a while until the identity API
is ready.

thanks

https://bugzilla.mozilla.org/show_bug.cgi?id=1305421

Putting my findings here for future sailors who might end up here searching for answers. https://github.com/mdn/webextensions-examples/tree/main/google-userinfo this example extension explains this really well.

It says copy the REDIRECT_URL from the about:addons > preferences but I couldn’t find it. Maybe it’s because the post is quite old (around 7 years). I used const REDIRECT_URL = browser.identity.getRedirectURL(); this instead, and it worked!