Using @discordjs/rest to make requests using Bearer access token

Is it possible to use the rest functions to fetch data for users with their OAuth access_token? Currently I've tried the following, the only option I got working was with a self made API call, I was wondering if I could utilize the REST class instead. DISCORD_CLIENT_TOKEN is the token for my bot/application accessToken is the bearer token for the user
const rest = new REST({ version: "10" }).setToken(DISCORD_CLIENT_TOKEN);
const rest2 = new REST({ version: "10" }).setToken(accessToken);

// Returns guilds for the application and not the user that the access token is for
const guilds = await rest.get(Routes.userGuilds(), {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});

// DiscordAPIError[0]: 401: Unauthorized
const guilds2 = await rest2.get(Routes.userGuilds(), {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});

// Error: Cannot use relative URL (/users/@me/guilds) with global fetch — use `event.fetch` instead: https://kit.svelte.dev/docs/web-standards#fetch-apis
const guilds3 = await fetch(Routes.userGuilds(), {
headers: {
Authorization: `Bearer ${accessToken}`,
},
}).then((res) => res.json());

// This works
const guilds4 = await fetch("https://discord.com/api/users/@me/guilds", {
headers: {
Authorization: `Bearer ${accessToken}`,
},
}).then((res) => res.json());
const rest = new REST({ version: "10" }).setToken(DISCORD_CLIENT_TOKEN);
const rest2 = new REST({ version: "10" }).setToken(accessToken);

// Returns guilds for the application and not the user that the access token is for
const guilds = await rest.get(Routes.userGuilds(), {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});

// DiscordAPIError[0]: 401: Unauthorized
const guilds2 = await rest2.get(Routes.userGuilds(), {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});

// Error: Cannot use relative URL (/users/@me/guilds) with global fetch — use `event.fetch` instead: https://kit.svelte.dev/docs/web-standards#fetch-apis
const guilds3 = await fetch(Routes.userGuilds(), {
headers: {
Authorization: `Bearer ${accessToken}`,
},
}).then((res) => res.json());

// This works
const guilds4 = await fetch("https://discord.com/api/users/@me/guilds", {
headers: {
Authorization: `Bearer ${accessToken}`,
},
}).then((res) => res.json());
4 Replies
d.js toolkit
d.js toolkit15mo 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! - Marked as resolved by OP
Loop
LoopOP15mo ago
Node Version: v18.17.1
├─┬ @discordjs/[email protected]
│ └── [email protected]
├─┬ @discordjs/[email protected]
│ └── [email protected]
how would I go about making the requests use the users bearer token with that? I can't find a way to configure it to do that, this results in a 401 as well
const rest = new REST({
version: "10",
headers: {
Authorization: `Bearer ${accessToken}`,
},
}).setToken(accessToken);
const api = new API(rest);

let guilds = await api.users.getGuilds();
const rest = new REST({
version: "10",
headers: {
Authorization: `Bearer ${accessToken}`,
},
}).setToken(accessToken);
const api = new API(rest);

let guilds = await api.users.getGuilds();
space
space15mo ago
Try removing the headers option and use authPrefix: 'Bearer' instead.
Loop
LoopOP15mo ago
oh perfect, thank you! this works now 😄
const rest = new REST({
version: "10",
authPrefix: "Bearer",
}).setToken(accessToken);
const api = new API(rest);

let guilds= await api.users.getGuilds();
const rest = new REST({
version: "10",
authPrefix: "Bearer",
}).setToken(accessToken);
const api = new API(rest);

let guilds= await api.users.getGuilds();
Want results from more Discord servers?
Add your server