如何在谷歌开发者控制台中将火狐扩展redirect_uri列入白名单



我正在制作Firefox扩展并实现google身份验证,问题是Firefox扩展(browser.identity.getRedirectURL(((redirect_uri不允许在Google开发人员控制台中列入白名单,因此redirect_uri发生不匹配错误,将不胜感激。

首先,我在chrome扩展程序中实现了google身份验证,在那里我使用了(chrome。Identity.getAuthToken && chrome.identity.getProfileUserInfo(在chrome中,但这两个功能不适用于Firefox,对于Firefox,我已经在Google开发人员控制台中创建了Web应用程序项目,但问题是redirect_uri发生不匹配错误。Google 开发者项目未将生成的网址列入白名单(browser.identity.getRedirectURL(((

const redirectURL = browser.identity.getRedirectURL();
const clientID = "clientId";
const scopes = ["openid", "email", "profile"];
let authURL = `https://accounts.google.com/o/oauth2/auth? 

client_id=${clientID}&response_type=token&redirect_uri=${encodeURIComponent(r edirectURL(}&scope=${encodeURIComponent(scopes.join(' '((}'; return browser.identity.launchWebAuthFlow({ 交互式:真, 网址:授权网址 } , 函数 (done , err( { console.log('done' , done(; });

如何在白名单中添加扩展 URL,或任何其他解决方案来处理这种情况。

从 Firefox 86 开始,您可以使用特殊的环回地址作为重定向 URI,不需要通过 Google 进行域验证。此 URI 的格式为http://127.0.0.1/mozoauth2/[subdomain of URL returned by identity.getRedirectURL()]。下面是如何生成此 URI 的示例。

const baseRedirectUrl = browser.identity.getRedirectURL();
const redirectSubdomain = baseRedirectUrl.slice(0, baseRedirectUrl.indexOf('.')).replace('https://', '');
const redirectUri = 'http://127.0.0.1/mozoauth2/' + redirectSubdomain;

以下是直接引用自 MDN 文档的一些其他细节:

identity.getRedirectURL()返回固定域名的 URL 和 从加载项的 ID 派生的子域。某些 OAuth 服务器(例如 Google( 仅接受具有经过验证的所有权的域作为重定向 网址。由于虚拟域无法由扩展开发人员控制, 不能始终使用默认域。

但是,环回地址是可接受的替代方法,不会 需要域验证(基于 RFC 8252 第 7.3 节(。开始 来自 Firefox 86,格式为 允许http://127.0.0.1/mozoauth2/[subdomain of URL returned by identity.getRedirectURL()]作为重定向的值 网址。

注意:从 Firefox 75 开始,您必须使用返回的重定向 URL 由identity.getRedirectURL().早期版本允许您提供 任何重定向网址。

从 Firefox 86 开始,上述特殊环回地址 也可以使用。