Authentication chrome.identity.launchWebAuthFlow能否用于针对Google API进行身份验证?
我正在编写一个Chrome扩展,并一直在尝试使用Chrome.identity.launchWebAuthFlow与Google进行身份验证。与chrome.identity.getAuthToken(确实有效)相比,我更喜欢这个,因为getAuthToken为当前登录到chrome的用户获取令牌——该用户可能登录到多个Google帐户。我希望用户能够将一个特定的Google日历连接到我的扩展,并且该日历可能属于不同的用户,而不是他们登录到Chrome的用户 因此,我一直在尝试使用chrome.identity.launchWebAuthFlow来实现这一点,但通常在重定向uri不匹配的情况下失败。我已经尝试了您可以在GoogleAPI开发人员控制台中设置的每种类型的凭据。(“Chrome应用程序”似乎是对的,但我也尝试过Web应用程序、Other和iOS。)我尝试过使用Chrome.extension.getURL('string')和Chrome.App.getRedirectURL('string')的结果作为我的重定向uri 我试过的例子应用程序提到的,但也无法得到工作 我怀疑我正在尝试做一些过去被允许但现在已经不存在的事情,或者根本不起作用 下面是我的代码示例,但我认为我的问题确实存在于API开发控制台中——我看不到一种方法可以设置一个适用于扩展的配置:Authentication chrome.identity.launchWebAuthFlow能否用于针对Google API进行身份验证?,authentication,google-chrome-extension,google-api,oauth-2.0,Authentication,Google Chrome Extension,Google Api,Oauth 2.0,我正在编写一个Chrome扩展,并一直在尝试使用Chrome.identity.launchWebAuthFlow与Google进行身份验证。与chrome.identity.getAuthToken(确实有效)相比,我更喜欢这个,因为getAuthToken为当前登录到chrome的用户获取令牌——该用户可能登录到多个Google帐户。我希望用户能够将一个特定的Google日历连接到我的扩展,并且该日历可能属于不同的用户,而不是他们登录到Chrome的用户 因此,我一直在尝试使用chrome.
var auth_url = 'https://accounts.google.com/o/oauth2/v2/auth';
var client_key = *[client id from API dev console]*
var auth_params = {
client_id: client_key,
redirect_uri: chrome.identity.getRedirectURL("oauth2.html")
scope: 'https://www.googleapis.com/auth/calendar'
};
auth_url += '?' + $.param(auth_params);
chrome.identity.launchWebAuthFlow({url: auth_url, interactive: true}, function(token) { console.log(token); });
(我也尝试过端点。)
解决方案:
在阅读了公认的答案后,我得出以下结论:
var auth_url = 'https://accounts.google.com/o/oauth2/auth';
var client_id = '[client ID from console]';
var redirect_url = chrome.identity.getRedirectURL("oauth2.html");
var auth_params = {
client_id: client_id,
redirect_uri: redirect_url,
response_type: 'token',
scope: 'profile'
};
auth_url += '?' + $.param(auth_params);
console.log(auth_url);
chrome.identity.launchWebAuthFlow({url: auth_url, interactive: true}, function(responseUrl) { console.log(responseUrl); });
responseUrl是我的带有参数的redirect_uri——因此Google oauth返回了它,而不是将浏览器重定向到它——我可以从那里继续 要开始运行,我需要:
https://bcgajjfnjjgadphgiodlifoaclnemcbk.chromiumapp.org/oauth2
该示例中获取重定向URI的调用类似于chrome.identity.getRedirectURL(“oauth2”),字符串参数根据扩展ID追加到URL的末尾。是的,2019年它仍然有效。终于让它工作了 manifest.json
{
"name": "Extension Name",
"description": "Description",
"version": "1.0.0",
"manifest_version": 2,
"icons": {
"48": "icons/icon_48.png",
"128": "icons/icon_128.png"
},
"background": {
"scripts": [
"background.js"
],
"persistent": false
},
"oauth2": {
"client_id": "Your Client ID from Google Develpers console (Must be Web Application)",
"scopes": [
"openid", "email", "profile"
]
},
"permissions": [
"identity"
],
"key": "Your Key from Google Developer Dashboard"
}
background.js
chrome.windows.create({
'url': './content/auth/auth.html',
'width': 454,
'height': 540,
'type': 'popup'
});
var auth_url = 'https://accounts.google.com/o/oauth2/auth?';
var client_id = '<Client ID>'; // must be Web Application type
var redirect_url = chrome.identity.getRedirectURL(); // make sure to define Authorised redirect URIs in the Google Console such as https://<-your-extension-ID->.chromiumapp.org/
var auth_params = {
client_id: client_id,
redirect_uri: redirect_url,
response_type: 'token',
scope: 'https://mail.google.com/',
login_hint: 'real_email@gmail.com' // fake or non-existent won't work
};
const url = new URLSearchParams(Object.entries(auth_params));
url.toString();
auth_url += url;
chrome.identity.launchWebAuthFlow({url: auth_url, interactive: true}, function(responseUrl) {
console.log(responseUrl);
});
auth.html
standard HTML markup that calls auth.js file
auth.js
chrome.windows.create({
'url': './content/auth/auth.html',
'width': 454,
'height': 540,
'type': 'popup'
});
var auth_url = 'https://accounts.google.com/o/oauth2/auth?';
var client_id = '<Client ID>'; // must be Web Application type
var redirect_url = chrome.identity.getRedirectURL(); // make sure to define Authorised redirect URIs in the Google Console such as https://<-your-extension-ID->.chromiumapp.org/
var auth_params = {
client_id: client_id,
redirect_uri: redirect_url,
response_type: 'token',
scope: 'https://mail.google.com/',
login_hint: 'real_email@gmail.com' // fake or non-existent won't work
};
const url = new URLSearchParams(Object.entries(auth_params));
url.toString();
auth_url += url;
chrome.identity.launchWebAuthFlow({url: auth_url, interactive: true}, function(responseUrl) {
console.log(responseUrl);
});
var auth\u url='0https://accounts.google.com/o/oauth2/auth?';
var客户机_id=“”;//必须是Web应用程序类型
var redirect_url=chrome.identity.getRedirectURL();//确保在Google控制台中定义授权重定向URI,例如https://.chromiumapp.org/
var auth_参数={
客户id:客户id,
重定向\u uri:重定向\u url,
响应类型:“令牌”,
范围:'https://mail.google.com/',
登录提示:'real'_email@gmail.com“//伪造或不存在都不起作用
};
constURL=新的URLSearchParams(Object.entries(auth_params));
url.toString();
auth_url+=url;
launchWebAuthFlow({url:auth\uURL,interactive:true},函数(responseUrl){
控制台日志(responseUrl);
});
请选择主题中的问题:包括与问题重复的完整问题。通常,包括manifest.json、一些后台脚本和内容脚本。寻求调试帮助的问题(“为什么此代码不工作?”)必须包括:►想要的行为,►特定的问题或错误,以及►在问题本身中复制它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:“如何创建”,以及。此方法是否仍然适用于您?由于某些原因,我仍然收到一个错误:未选中的运行时。lastError:无法加载授权页。我创建了Web应用程序ID,并遵循上述步骤。两个端点都很累,但都没有用。我是不是错过了一些线索?@Armand,我最终没有使用这个API,所以我不知道这是否仍然有效。我会试试看我是否能做到这一点,并尽快接受你的回答。我至少已经设法输入了一个您在Google开发者控制台中指定的表单的授权重定向URI。我以不同的方式构建了我的应用程序——扩展只与“我们的”服务器直接通信,“我们的”服务器通过oauth2代表用户使用Google API。我们需要服务器到服务器的通信。谢谢!我还没有回到角度样本,但你的提示帮助我克服了这个关键点。(谷歌可能也更新了他们的chrome.identity文档,因为现在很清楚,它没有像我几个月前所想的那样做)。除了开发人员控制台问题之外,我还需要向auth URL添加一个“response_type”参数。当使用chrome.identity.getRedirectURL('whatever')URL作为重定向uri时,传递给launchWebAuthFlow的回调会将带有查询参数的重定向uri作为其单个参数。