昨天我遇到了同样的问题,既然找到了解决方案,我不妨分享一下,因为它并不那么明显。据我所知,Google不提供直接和有记录的方式来执行此操作,但您可以使用chrome.identity.launchWebAuthFlow()
函数。
首先,你应该创建一个Web应用程序在谷歌控制台凭据并添加以下URL作为有效Authorized redirect URI
:https://<EXTENSION_OR_APP_ID>.chromiumapp.org
。 URI不一定存在,chrome只会捕获重定向到这个URL并稍后调用你的回调函数。
manifest.json的:
{
"manifest_version": 2,
"name": "name",
"description": "description",
"version": "0.0.0.1",
"background": {
"scripts": ["background.js"]
},
"permissions": [
"identity"
],
"oauth2": {
"client_id": "<CLIENT_ID>.apps.googleusercontent.com",
"scopes": [
"openid", "email", "profile"
]
}
}
background.js:
// Using chrome.identity
var manifest = chrome.runtime.getManifest();
var clientId = encodeURIComponent(manifest.oauth2.client_id);
var scopes = encodeURIComponent(manifest.oauth2.scopes.join(' '));
var redirectUri = encodeURIComponent('https://' + chrome.runtime.id + '.chromiumapp.org');
var url = 'https://accounts.google.com/o/oauth2/auth' +
'?client_id=' + clientId +
'&response_type=id_token' +
'&access_type=offline' +
'&redirect_uri=' + redirectUri +
'&scope=' + scopes;
chrome.identity.launchWebAuthFlow(
{
'url': url,
'interactive':true
},
function(redirectedTo) {
if (chrome.runtime.lastError) {
// Example: Authorization page could not be loaded.
console.log(chrome.runtime.lastError.message);
}
else {
var response = redirectedTo.split('#', 2)[1];
// Example: id_token=<YOUR_BELOVED_ID_TOKEN>&authuser=0&hd=<SOME.DOMAIN.PL>&session_state=<SESSION_SATE>&prompt=<PROMPT>
console.log(response);
}
}
);
谷歌的OAuth2 API(用于ID连接)文档可以在这里找到:https://developers.google.com/identity/protocols/OpenIDConnect#authenticationuriparameters
PS :如果您不需要清单中的oauth2部分。您可以放心地忽略它,并仅以代码形式提供标识符和范围。
编辑: 对于那些有兴趣,你不需要身份的API。你甚至可以使用tab API使用一些小技巧来访问令牌。代码有点长,但是你有更好的错误信息和控制。请记住,在以下示例中,您需要创建Chrome App凭据。
manifest.json的:
{
"manifest_version": 2,
"name": "name",
"description": "description",
"version": "0.0.0.1",
"background": {
"scripts": ["background.js"]
},
"permissions": [
"tabs"
],
"oauth2": {
"client_id": "<CLIENT_ID>.apps.googleusercontent.com",
"scopes": [
"openid", "email", "profile"
]
}
}
background.js:
// Using chrome.tabs
var manifest = chrome.runtime.getManifest();
var clientId = encodeURIComponent(manifest.oauth2.client_id);
var scopes = encodeURIComponent(manifest.oauth2.scopes.join(' '));
var redirectUri = encodeURIComponent('urn:ietf:wg:oauth:2.0:oob:auto');
var url = 'https://accounts.google.com/o/oauth2/auth' +
'?client_id=' + clientId +
'&response_type=id_token' +
'&access_type=offline' +
'&redirect_uri=' + redirectUri +
'&scope=' + scopes;
var RESULT_PREFIX = ['Success', 'Denied', 'Error'];
chrome.tabs.create({'url': 'about:blank'}, function(authenticationTab) {
chrome.tabs.onUpdated.addListener(function googleAuthorizationHook(tabId, changeInfo, tab) {
if (tabId === authenticationTab.id) {
var titleParts = tab.title.split(' ', 2);
var result = titleParts[0];
if (titleParts.length == 2 && RESULT_PREFIX.indexOf(result) >= 0) {
chrome.tabs.onUpdated.removeListener(googleAuthorizationHook);
chrome.tabs.remove(tabId);
var response = titleParts[1];
switch (result) {
case 'Success':
// Example: id_token=<YOUR_BELOVED_ID_TOKEN>&authuser=0&hd=<SOME.DOMAIN.PL>&session_state=<SESSION_SATE>&prompt=<PROMPT>
console.log(response);
break;
case 'Denied':
// Example: error_subtype=access_denied&error=immediate_failed
console.log(response);
break;
case 'Error':
// Example: 400 (OAuth2 Error)!!1
console.log(response);
break;
}
}
}
});
chrome.tabs.update(authenticationTab.id, {'url': url});
});
通过OAuth的术语是疯了。在你的文章中,你指的是“用户ID”,“client_id”,“id令牌”和“id”。你能确切地知道你想要得到什么吗? (我知道关于“client_id”的一些事情,“id”太泛泛,我不明白,而且我不确定什么是“用户ID”。) – 2014-10-08 19:46:46
同意,有几个术语需要保留分离。为了清晰起见编辑了该帖子。在文章的第一个链接中提到了client_id,需要在id_token中指定观众。我想很明显,我想要一个可以验证的id_token,可以在追加到https://www.googleapis.com/oauth2/v1/tokeninfo?id_token= – chris 2014-10-09 06:38:18
时尝试将“openid”添加到范围吗?这通常是使服务器进入OpenID Connect模式并发出ID令牌的必要提示。 – 2018-01-23 10:43:48