Slack API chat.update returns 'not_authed' error

Jamie picture Jamie · Dec 6, 2016 · Viewed 12.6k times · Source

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.

Answer

Walery Strauch picture Walery Strauch · Oct 26, 2018

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.