Slack API chat.update returns 'not_authed' error

18,374

Solution 1

Ok! thankyou all for your input.. certainly I have learned a little more. After tweeting at slack_api my original code more or less worked as is.. I had to JSON.parse(payload); the payload in order to then access the object parameters within.. the full example is as below.

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

}

Solution 2

You need to put token to header instead of json payload if using application/json. Here is doc for this.

So you request should look like this:

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

Note: there is no token field in payload.

Solution 3

Well according to the link your provided, Slack does not accept JSON data (weird).

Also, after playing around with their tester, Slack seems to be doing a GET request on https://slack.com/api/chat.update with query parameters attached like this:

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

So use this code:

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);
Share:
18,374

Related videos on Youtube

Jamie
Author by

Jamie

Technical artist specialising in Maya, Python, PySide, Animation, Game Development, and Education.

Updated on October 26, 2020

Comments

  • Jamie
    Jamie over 3 years

    I have a google script running as a webapp to handle the backend of a slack app.

    The app has been Authenticated and I have the OAUTH token from this.

    I can currently post to a channel with button actions using the chat.postMessage using the for-mentioned token.

    Actions url points back at my webapp and hook in via doGet, from this response i construct a JSON object.

    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)
    

    posted via the following function:

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

    however returned is the following:

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

    I can't find any documentation about this error other than the following

    Sending JSON to Slack in a HTTP POST request

    However this is in regard to a chat.postMessage request of which in my implementation is working correctly.

  • Jamie
    Jamie over 7 years
    Thanks for this.. I tried this out and modifications of it and got no where.. mysteriously after tweeting at slack about it.. my original script started working..
  • Apoorv Kansal
    Apoorv Kansal over 7 years
    Ah right very very odd. I believe the Slack team are in the middle of supporting json data and probably heard you!
  • Jamie
    Jamie over 7 years
    Yeah bro.. I don't know.. they said that they don't.. but all of a sudden it kinda works.. I don't find their API documentation to be very good at all
  • Leo Fisher
    Leo Fisher about 3 years
    This helped, ended up finding this api.slack.com/web#slack-web-api__basics__post-bodies for further verification

Related