Home Articles

Slack WebAPI因not_authed而失败

Asked
Viewed 1327 times
2

我试图将交互式消息作为Bot用户发布(使用chat.postMessage等) .

虽然我正在传递Bot访问令牌(从最初的OAuth收到),但我仍然收到一条错误响应,声明“not_authed” .

当我尝试auth.test时,我得到了同样的结果 .

我在node.js中使用“request”执行以下操作:

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"); 
});

结果如下:

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

根据Slack WebAPI文档,如果我作为Bot发布,我应该使用Bot的访问令牌(从初始oauth收到),但是我想要正确地格式化我的请求,或者令牌不是什么Slack期待 .

1 Answer

  • 2

    好吧,在与Slack支持人员交谈之后,它(至少)显示我正在调用的WebAPI还不支持application / json . 这些可以使用x-www-form-urlencoded .

    看着这个post

    我能够拼凑出成功编写的以下内容:

    //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"); 
    });
    

Related