简体   繁体   English

尝试在后端nodejs服务器上验证时,在js chrome扩展中生成令牌时出错

[英]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.

相关问题 验证安装Chrome扩展程序时出错 - Error when verify install chrome extension 在JS中将Firebase添加到Chrome扩展程序时出错 - Error when adding Firebase to Chrome extension in JS 尝试使用Chrome扩展程序中的SignalR连接到集线器时出错 - Error when trying to connect to hub with SignalR in a Chrome extension 尝试在chrome扩展名中获取表单元素的值时出错 - Error when trying to acess value of a form element in a chrome extension 尝试验证节点 js 中的令牌 - Trying to verify a token in node js NodeJS:服务器返回“没有当前用户”。 尝试在服务器端引用 ID 令牌时 - NodeJS: Server returning “There is no current user.” when trying to reference ID token server-side Chrome 扩展客户端显示成功但服务器生成 500 错误 - Chrome extension client side showing successful but server generating 500 error Ionic 3:验证令牌是否在nodeJS服务器上过期 - Ionic 3 : verify if token has expired on nodeJS Server 运行server.js时NodeJS要求错误 - NodeJS require error when running the server.js Chrome扩展程序:尝试获取选项卡URL时,Background.js“未捕获的TypeError” - Chrome Extension: Background.js “Uncaught TypeError” when trying to get tab url
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM