[英]discord api add user to guild
Hi I am trying to use discord api to add the user to the guild when he login.嗨,我正在尝试使用 discord api 在用户登录时将其添加到公会。 But I am getting 401 Unauthorized and I am not sure why.
但我得到 401 Unauthorized 我不知道为什么。 This is my code:
这是我的代码:
const data = new FormData();
data.append("client_id", CLIENT_ID);
data.append("client_secret", CLIENT_SECRET);
data.append("grant_type", "authorization_code");
data.append("redirect_uri", `http://localhost:3000/callback`);
data.append("scope", "identify,guilds.join");
data.append("code", req.query.code);
var response = await fetch("https://discord.com/api/oauth2/token", {
method: "POST",
body: data
});
const json = await response.json();
var resp = await fetch(`https://discord.com/api/guilds/813847215058845708/members/463696108766494730`, {
method: "PUT",
headers: {
"Authorization": `Bot ${json.access_token}`,
"Content-Type": "application/json",
}
});
console.log(resp)控制台.log(resp)
Everything seems to be working,I can get the user avatar,name and etc. but can't add him to the guild,anyone help me,plz一切似乎都在工作,我可以获得用户头像,姓名等,但无法将他添加到公会,任何人都可以帮助我,请
You're providing an invalid authorization header.您提供的授权无效 header。 You can't use your access token with the
bot
prefix since you're making a normal OAuth2 call.由于您正在进行正常的 OAuth2 调用,因此您不能使用带有
bot
前缀的访问令牌。 Instead you need to provide the token type returned by the Discord-API.相反,您需要提供 Discord-API 返回的令牌类型。
Change改变
headers: {
"Authorization": `Bot ${json.access_token}`,
"Content-Type": "application/json",
}
to至
headers: {
authorization: `${json.token_type} ${json.access_token}`
}
Also, the way you pass your scopes seems to be incorrect as well.此外,您传递范围的方式似乎也不正确。 Try
尝试
data.append("scope", "identify guilds guilds.join");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.