2

I'm attempting to post interactive messages to slack as a Bot User (using chat.postMessage, etc).

Although I am passing in the Bot Access Token (as received from the initial OAuth) I keep getting an error response stating "not_authed".

I get the same when I attempt auth.test.

I'm doing something like the following with "request" in node.js:

app.get("/testAuth/test", function(req,res){
    console.log("in testAuth/test...sending test message to Slack");

    var bToken = process.env.TESTBOT_ACCESS_TOKEN;
    var slackMessageURL = "https://slack.com/api/auth.test";
    var postOptions = {
        uri: slackMessageURL,
        method: "POST",
        token: bToken
    };
    request(postOptions, (error, response, body) => {
        if(error){
            console.log("OOPPPPS....we hit an error in auth.test: " + error);
        } else {
            console.log("auth.test response: " + JSON.stringify(response));
        }
    });
    res.send("Sent Test...check logs"); 
});

which results with:

auth.test response: {"statusCode":200,"body":"{\"ok\":false,\"error\":\"not_authed\"}",...

According to the Slack WebAPI docs, if I'm posting as the Bot, I should use the Bot's access token (as received from the initial oauth), but figure I'm either formatting my request incorrectly, or the token is not what Slack is expecting.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
gMoney
  • 111
  • 1
  • 7
  • Testing with Postman, it seems I'm able to successfully auth when the token is passed x-www-form-urlencoded. – gMoney Feb 02 '17 at 19:57

1 Answers1

2

Ok, after talking with Slack support, it appears (at least) the WebAPIs I am calling don't yet support application/json. These do work with x-www-form-urlencoded.

Looking at this post

I was able to cobble together the following which auth'd successfully:

//up top
var request = require("request");

var querystring = require("querystring");

//...

app.get("/testAuth/test", function(req,res){
    console.log("in testAuth/test...sending test message to Slack");

    var bToken = process.env.TESTBOT_ACCESS_TOKEN;
    var message = {
        token: bToken
    };
    var messageString = querystring.stringify(message);
    var messageLength = messageString.length;
    var slackMessageURL = "https://slack.com/api/auth.test";
    var postOptions = {
        headers: {
            "Content-length": messageLength,
            "Content-type": "application/x-www-form-urlencoded"
        },
        uri: slackMessageURL,
        body: messageString,
        method: "POST"
    };
    request(postOptions, (error, response, body) => {
        if(error){
            console.log("OOPPPPS....we hit an error in auth.test: " + error);
        } else {
            console.log("auth.test response: " + JSON.stringify(response));
        }
    });
    res.send("Sent Test...check logs"); 
});
Community
  • 1
  • 1
gMoney
  • 111
  • 1
  • 7