DiscordAPIError[0]: 401: Unauthorized even though im authorized and the access token is brand new?

I want to get user's @me info with access token, but it says im unauthorized?
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v10');
const rest = new REST({ version: '10', authPrefix:'Bearer' }).setToken(req.cookies.__access_token__);

const info = await rest.get(Routes.user())
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v10');
const rest = new REST({ version: '10', authPrefix:'Bearer' }).setToken(req.cookies.__access_token__);

const info = await rest.get(Routes.user())
8 Replies
d.js toolkit
d.js toolkit4mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
jimi
jimiOP4mo ago
throw new DiscordAPIError(data, "code" in data ? data.code : data.error, res.status, method, url, requestData);
^

DiscordAPIError[0]: 401: Unauthorized
at SequentialHandler.runRequest (C:\Users\julus\OneDrive\Desktop\levelio\node_modules\@discordjs\rest\dist\index.js:708:15)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async SequentialHandler.queueRequest (C:\Users\julus\OneDrive\Desktop\levelio\node_modules\@discordjs\rest\dist\index.js:511:14)
at async C:\Users\julus\OneDrive\Desktop\levelio\index.js:58:18 {
rawError: { message: '401: Unauthorized', code: 0 },
code: 0,
status: 401,
method: 'get',
url: 'https://discord.com/api/v10/users/@me',
requestBody: { files: undefined, json: undefined }
}

Node.js v21.6.0
throw new DiscordAPIError(data, "code" in data ? data.code : data.error, res.status, method, url, requestData);
^

DiscordAPIError[0]: 401: Unauthorized
at SequentialHandler.runRequest (C:\Users\julus\OneDrive\Desktop\levelio\node_modules\@discordjs\rest\dist\index.js:708:15)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async SequentialHandler.queueRequest (C:\Users\julus\OneDrive\Desktop\levelio\node_modules\@discordjs\rest\dist\index.js:511:14)
at async C:\Users\julus\OneDrive\Desktop\levelio\index.js:58:18 {
rawError: { message: '401: Unauthorized', code: 0 },
code: 0,
status: 401,
method: 'get',
url: 'https://discord.com/api/v10/users/@me',
requestBody: { files: undefined, json: undefined }
}

Node.js v21.6.0
Yes
jimi
jimiOP4mo ago
yeah
No description
jimi
jimiOP4mo ago
theres no reason for it to throw 401
jimi
jimiOP4mo ago
i console log it right before running rest
No description
jimi
jimiOP4mo ago
and it logs a valid token do u know what i could do to fix this yeah now i know what's wrong, the authorization header begins with the bot prefix instead of bearer even tho i set it to bearer 0.4.1 i installed it today? i didnt add any version parameter i just ran npm i @discordjs/rest so i thought its gonna install @latest just updated it to 2.3.0 and it still uses bot instead of bearer
jimi
jimiOP4mo ago
No description
jimi
jimiOP4mo ago
ohh nvm it works now thanks a lot
Want results from more Discord servers?
Add your server