Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slack API returning with invalid_auth when running GET from HTML?

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:

  1. token= xoxb-xxxx...AwQ
  2. channel= xxxxH9P
  3. The Bot is added to the channel.
  4. I have the correct Scopes added.
  5. 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

like image 560
dannington Avatar asked May 09 '26 19:05

dannington


1 Answers

Investigating further with just curl, I was able to GET output with either bot/channel using the following format:

curl -H "Authorization: Bearer xoxb-xxxxxAwQ" -X GET 'https://slack.com/api/conversations.history?channel=CxxxxTX'

Still unclear to me why the other API call format works for my original bot, but I'm happy to accept and use this format, and to adjust the HTML code to accordingly.

Thanks! Dann

like image 111
dannington Avatar answered May 11 '26 08:05

dannington