[英]Error when generating a token in a js chrome extension when trying to verify it on a backend nodejs server
I am trying to generate a token in a chrome extension and then use it to verify requests to the backend server. 我试图在chrome扩展中生成一个令牌,然后使用它来验证对后端服务器的请求。 I have been successful at generating a token in the front end but when I send it to the backend and verify it with the nodejs library
google-auth-library
but always get the error Error: Wrong number of segments in token: [TOKEN_HERE]
. 我已经成功地在前端生成令牌但是当我将它发送到后端并使用nodejs库
google-auth-library
验证它时,但总是得到错误Error: Wrong number of segments in token: [TOKEN_HERE]
。
I have tried to find out how to fix it online and nothing that I have found works. 我试图找出如何在线修复它,我发现的任何东西都没有。 The ID of my extension is the same as the ID in the console.
我的扩展程序的ID与控制台中的ID相同。
I created a chrome extension application in the developer console and added this to my manifest: 我在开发者控制台中创建了一个chrome扩展应用程序,并将其添加到我的清单中:
"oauth2": {
"client_id": "THE ID",
"scopes":["https://www.googleapis.com/auth/classroom.topics.readonly", "https://www.googleapis.com/auth/classroom.topics","https://www.googleapis.com/auth/classroom.courses.readonly", "https://www.googleapis.com/auth/classroom.rosters.readonly", "https://www.googleapis.com/auth/classroom.student-submissions.me.readonly"]
},
...
"permissions": [
"identity",
],
Once I ran the code 一旦我运行代码
chrome.identity.getAuthToken({interactive: true}, function(token) {
console.log(token);
});
It generates a token that I was able to validate in the extension with: 它生成一个我可以在扩展中验证的令牌:
var x = new XMLHttpRequest();
x.open('GET', 'https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=' + token);
This also generated a new oAuth2 client in my console that is a web application: https://i.imgur.com/uH789P8.png 这也在我的控制台中生成了一个新的oAuth2客户端,它是一个Web应用程序: https ://i.imgur.com/uH789P8.png
On the backend nodejs server I tried using both of the generated IDs to verify the token. 在后端nodejs服务器上,我尝试使用两个生成的ID来验证令牌。 The extension application doesn't have a client secret but the webapp does.
扩展应用程序没有客户端密钥,但webapp有。 I have tried using the secret with the webserver id but this didn't work either.
我已尝试使用网络服务器ID的秘密,但这也无效。
This the code for the backend verification: 这是后端验证的代码:
const { OAuth2Client } = require('google-auth-library');
const authClient = new OAuth2Client(CLIENT_ID);
async function verify(token) {
const ticket = await authClient.verifyIdToken({
idToken: token,
audience: [CLIENT_ID] // Specify the CLIENT_ID of the app that accesses the backend
// Or, if multiple clients access the backend:
//[CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3]
});
const payload = ticket.getPayload();
return {
domain: payload['hd'],
userid: payload['sub']
}
}
I want to verify the token with this code but it is unable to do so. 我想用这段代码验证令牌,但是无法这样做。 Every time I use the token that is verifiable with the
xhr
request it just gives me the error Error: Wrong number of segments in token
. 每次我使用可以通过
xhr
请求验证的令牌时,它只会给我错误Error: Wrong number of segments in token
。 I have no clue where to go from here as documentation is a bit scarce. 我不知道从哪里开始,因为文档有点稀缺。 All help is appreciated!
所有帮助表示赞赏!
EDIT: I have also tried to validate the token with the Bearer
prefix it didn't work. 编辑:我也尝试使用
Bearer
前缀验证令牌它不起作用。
EDIT 2: I have figured out the problem! 编辑2:我已经找到了问题! ... just not the solution.
......不是解决方案。 Will update when I figure it out.
我想出来的时候会更新。 Just so you know the reason why it doesn't work is that the way I tried getting the token gave me an access token and not an ID token.
只是因为你知道它不起作用的原因是我尝试获取令牌的方式给了我一个访问令牌而不是ID令牌。 I am going to try and find out how to verify access tokens and more.
我将尝试找出如何验证访问令牌等。
If you are looking to verify an access token (what is returned from chrome.identity) on a backend server then use this code: 如果您要在后端服务器上验证访问令牌(从chrome.identity返回的内容),请使用以下代码:
const { OAuth2Client } = require('google-auth-library');
const authClient = new OAuth2Client(CLIENT_ID, CLIENT_SECRET);
authClient.getTokenInfo(token)
If authClient.getTokenInfo(token) errors out then the token is not valid. 如果authClient.getTokenInfo(token)出错,则令牌无效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.