Slack API chat.update返回' not_authed'错误

时间:2016-12-06 02:26:32

标签: json google-apps-script slack-api

我有一个 google脚本作为 webapp 运行,以处理松弛应用的后端。

该应用已经经过身份验证,我从中获得了OAUTH 令牌

我目前可以使用 chat.postMessage 使用上述令牌发布按钮操作的频道。

操作url指向我的webapp并通过doGet挂钩,从这个响应我构造了一个JSON对象。

var response_payload = {
    "token" : access_token,
    "ts" : message_ts,
    "channel" : channel_id,
    "text" : "Approved! you are a winner!"
  })

response_url = "https://slack.com/api/chat.update";

sendToSlack_(response_url, response_payload)

通过以下功能发布:

function sendToSlack_(url,payload) {
   var options =  {
    "method" : "post",
    "contentType" : "application/json;charset=iso-8859-1",
    "payload" : JSON.stringify(payload)
  };
  return UrlFetchApp.fetch(url, options)
}

然而返回如下:

{"ok":false,"error":"not_authed"}

除了以下

之外,我无法找到有关此错误的任何文档

Sending JSON to Slack in a HTTP POST request

然而,这是关于我的实现中正常工作的chat.postMessage请求。

3 个答案:

答案 0 :(得分:1)

根据您提供的链接,Slack不接受JSON数据(很奇怪)。

此外,在使用他们的测试人员之后,Slack似乎在GET上发出https://slack.com/api/chat.update请求,其中附带的查询参数如下:

https://slack.com/api/chat.update?token=YOUR_TOKEN&ts=YOUR_TIME&channel=YOUR_CHANNEL&text=YOUR_TEXT_URL_ENCODED&pretty=1

所以使用这段代码:

var response_payload = {
    "token" : access_token,
    "ts" : message_ts,
    "channel" : channel_id,
    "text" : "Approved! you are a winner!"
  }


function httpGet(theUrl)
{
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
    xmlHttp.send( null );
    return xmlHttp.responseText;
}

response_url = encodeURI("https://slack.com/api/chat.update?token=" + response_payload['token'] + 
"&ts=" + response_payload['ts'] + "&channel=" + response_payload['channel'] + "&text=" + response_payload['text'] +"&pretty=1");

httpGet(response_url);

答案 1 :(得分:1)

如果使用application/json,则需要将令牌放在标头而不是json负载中。这是doc

因此您的请求应如下所示:

POST /api/chat.update HTTP/1.1
Authorization: Bearer xoxp-xxx-xxx-xxx-xxx
Content-Type: application/json;charset=UTF-8

{
    "channel": "xxx",
    "text": "Hello ~World~ Welt",
    "ts": "xxx"
}

注意:有效载荷中没有token字段。

答案 2 :(得分:0)

确定!谢谢你的意见。当然,我已经学到了更多。在slack_api发推文后,我的原始代码或多或少地按原样运行。我必须JSON.parse(payload);有效负载才能访问其中的对象参数..完整示例如下所示。

function post_update(url, payload) {
  var options =
  {
    'method': 'post',
    "payload" : payload,
  };

  var result = UrlFetchApp.fetch(url, options);
  return result.getContentText();
}

function doPost(e) {
  var payload = e.parameter.payload;
  var json = JSON.parse(payload);

  response_url = "https://slack.com/api/chat.update";

  // get object elements
  var action = json.actions[0].value;
  var user = json["user"].name;
  var message_ts = json["message_ts"];
  var channel_id = json["channel"].id;

  if (action == 'approved') // payload if action is 'approved'
  {
    var response_payload = {
      "token" : access_token,
      "ts" : message_ts,
      "channel" : channel_id,
      "text" : "Approved! *" + invitation_name + "* has been sent an invite!",
      "attachments" : JSON.stringify([{
          "text": ":white_check_mark: Approved by @" + user,
           }])
      }
  }

  if (action == 'denied') // payload if action is 'denied'
  {
    var response_payload = {
      "token" : access_token,
      "ts" : message_ts,
      "channel" : channel_id,
      "text" : "Denied. *" + invitation_name + "* has been declined an invite",
      "attachments" :JSON.stringify([{
          "text": ":exclamation: Declined by @" + user,
          }])
    }
  }

  post_update(response_url, response_payload);
  return ContentService.createTextOutput().setMimeType(ContentService.MimeType.JSON);

}