I am new to Javascript. Now I am working on a Chrome Extension. I need to realize ability to create a Discord guild channel by clicking link in the Chrome Extension.
I am successfully signing in via Discord in the Chrome Extension and getting access token. After this I'm trying to create channel, but always getting 401 Unauthorized.
But where I do GET request to show user guilds (/users/@me/guilds) it works, but when trying to GET channels (/users/@me/channels) I receiving only 401 unauthorized..
Could anybody explain what I'm doing wrong? Thank you in advance)
Here is my code: (scope from first request: guilds.join, identify, guilds, email, connections, 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).")
}
} );
});
The /users/@me/channels
is not for creating a guild channel, but for creating a DM channel. Also you shouldn't send a GET request to /users/@me/channels
, but a POST request as the documentation says. At this moment there is no possibility to create a guild channel with Discord oauth2 so unfortunately you won't be able to procees your extension.
Here are the Discord documentations:
https://discord.com/developers/docs/resources/user
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.