我是Java的新手。现在,我正在开发Chrome扩展程序。 我需要通过单击Chrome扩展程序中的链接来实现创建Discord公会频道的功能。
我已成功通过Chrome扩展程序中的Discord登录并获得了访问令牌。在此之后,我尝试创建频道,但始终获得401 Unauthorized。
但是我在执行GET请求以显示用户行会(/ users / @ me / guilds)的地方却有效,但是当尝试获取频道(/ users / @ me / channels)时,我只收到未经授权的401。 >
有人可以解释我在做什么错吗? 预先谢谢你
这是我的代码: (来自第一个请求的范围:guilds.join,标识,行会,电子邮件,连接,messages.read,bot,rpc,rpc.api)
$("#discord_sign_in").click(function () {
chrome.identity.launchWebAuthFlow(
{'url': 'https://discordapp.com/api/oauth2/authorize?client_id=*hidden*&redirect_uri=https%3A%2F%2Fgnbkehnofikpgioaajgmejnkihdkpiap.chromiumapp.org%2Fsettings%2Findex.html&response_type=code&scope=guilds.join%20identify%20guilds%20email%20connections%20messages.read%20bot%20rpc%20rpc.api',
'interactive': true},
function(redirect_url) {
console.log('Authorization success');
console.log(redirect_url);
// var redirect_url = redirect.replace("#", "?");
var url = new URL(redirect_url);
var code = url.searchParams.get("code");
console.log(code);
localStorage.setItem('discord_code', code);
console.log('get new code: ' + localStorage.getItem('discord_code'));
});
});
$("#discord_sign_in_bot").click(function() {
$.ajax( {
url: 'https://discordapp.com/api/v6/oauth2/token',
type: 'POST',
data: { grant_type: 'authorization_code',
scope: 'bot',
client_id: 'localStorage.getItem('client_id')',
client_secret: 'localStorage.getItem('client_secret')',
code: localStorage.getItem('discord_code'),
permissions: 8,
redirect_uri: 'https://gnbkehnofikpgioaajgmejnkihdkpiap.chromiumapp.org/settings/index.html' },
success: function( response ) {
console.log("Successfully authorized bot (Discord).")
console.log(response);
console.log("access_token: " + response['access_token']);
localStorage.setItem('discord_token', response['access_token']);
},
error: function () {
console.log("Failed to bot authorization (Discord).")
}
} );
});
$("#discord_create_channel").click(function() {
$.ajax( {
url: 'https://discordapp.com/api/v6/guilds/594100687508340776/channels/',
type: 'POST',
data: { name: 'Test-channel-5' },
beforeSend : function( xhr ) {
xhr.setRequestHeader( "Authorization", "Bot " + localStorage.getItem('discord_token'));
},
success: function( response ) {
console.log("Successfully created guild channel (Discord).")
},
error: function (response) {
console.log(response);
console.log("Failed to create guild channel (Discord).")
}
} );
});
答案 0 :(得分:0)
/users/@me/channels
不是用于创建公会频道,而是用于创建 DM 频道。此外,您不应该向 /users/@me/channels
发送 GET 请求,而是按照文档所述发送 POST 请求。目前无法使用 Discord oauth2 创建公会频道,因此很遗憾您将无法继续您的扩展。
以下是 Discord 文档:
https://discord.com/developers/docs/resources/user