I have created a Slack Bot in a Test workspace to play with. All I am trying to do is run a GET API to collect the content of a channel, but I am tripping up at the first hurdle...
The API = GET https://slack.com/api/conversations.history
Variables:
- token= xoxb-xxxx...AwQ
- channel= xxxxH9P
- The Bot is added to the channel.
- I have the correct Scopes added.
- Using the Tester API here actually works!
But my own HTML code fails.. returning with invalid_auth... So I guess it must be an issue with my HTML... But the thing is, the exact same code works with another slack bot I set up previously!
The basic HTML:
<html>
<head>
</head>
<body>
<button id = "SendButton" onclick="GetChannelFromSlack()">GET</button>
<p id="GET API Output">Test</p>
</body>
<script>
function GetChannelFromSlack() {
var slackBotToken = "xoxb-xxxxAwQ";
var slackChannelID = "xxxxH9P";
var getSlackData = new XMLHttpRequest();
var url = new URL('https://slack.com/api/conversations.history?token='+slackBotToken+'&channel='+slackChannelID);
getSlackData.open( "GET", url, true );
//getSlackData.setRequestHeader = ('Authorization', 'Bearer xoxb-xxxxAwQ');
getSlackData.responseType = 'json';
getSlackData.send();
getSlackData.onload = function() {
var jsonResponse = getSlackData.response;
document.getElementById("GET API Output").innerHTML = JSON.stringify(jsonResponse);
}
}
</script>
</html>
(Code snippet won't actually work as I have not entered in my actual bot token / channel id)
Edit Additionally, to isolate the issue - here is a curl command which works for one Bot/Channel, but not the other:
Working: curl -X GET 'https://slack.com/api/conversations.history?token=xoxb-xxx-VKa&channel=CxxxxTX'
Not Working: curl -X GET 'https://slack.com/api/conversations.history?token=xoxb-xxx-AwQ&channel=Cxxxx9P'
I do not see any difference in the setup of the two bots...
If anyone can identify why this is failing I would be really appreciate it!
Regards, Dann