chrome token在哪看_使用Chrome身份验证API获取id_token

本文介绍了如何在Google Chrome扩展中使用Chrome Identity API进行用户身份验证,并展示了如何通过该API获取OpenID Connect的ID_Token,包括设置manifest.json文件和编写后台脚本的详细步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I am developping a Google Chrome extension, to allow users to authenticate with their Google Accounts, i decided to use Chrome Identity API.

To authenticate the user in my Application i need to get the ID_Token (signed token)

is there a way to get OpenID Connect Token with Google Chrome Identity API ?

Thanks for your help !

解决方案

This is a paste of my answer from the other thread https://stackoverflow.com/a/32548057/3065313

I've came to the same problem yesterday and since I've found a solution, I might as well share it, as it wasn't that obvious. As far as i know Google does not provide a direct and documented way to do this, but you can use the chrome.identity.launchWebAuthFlow() function.

First you should create an Web application credentials in google console and add the following url as a valid Authorized redirect URI: https://.chromiumapp.org. The URI does not have to exist, chrome will just catch the redirect to this URL and call your callback function later.

manifest.json:

{

"manifest_version": 2,

"name": "name",

"description": "description",

"version": "0.0.0.1",

"background": {

"scripts": ["background.js"]

},

"permissions": [

"identity"

],

"oauth2": {

"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=&authuser=0&hd=&session_state=&prompt=

console.log(response);

}

}

);

PS: If you don't need the oauth2 section in your manifest. You can safely omit it, and provide the identifiers and scopes in code only.

EDIT:

For those interested, you don't need the identity API. You can even access the token using a little trick with tabs API. The code is a little longer, but you have better error messages and control. Keep in mind that in the following example, you need to create Chrome App credentials.

manifest.json:

{

"manifest_version": 2,

"name": "name",

"description": "description",

"version": "0.0.0.1",

"background": {

"scripts": ["background.js"]

},

"permissions": [

"tabs"

],

"oauth2": {

"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=&authuser=0&hd=&session_state=&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});

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值