Chatroom tutorial part 3

Authenticating REST requests

The simplest way of authenticating REST request is using Basic Authentication. If you are using one of Scaledrone's API clients this has already been built in.

Turn on channel authentication from the channel admin page:
random.js
var request = require('request');

var basicAuth = 'Basic ' + new Buffer('channel_id' + ':' + 'secret_key').toString('base64');
// -> Basic Y2hhbm5lbF9pZDpzZWNyZXRfa2V5

setInterval(function () {
  var message = {
    admin: true,
    name: 'Random Number Roller',
    content: 'New random number is ' + Math.floor(Math.random() * 100)
  }

  request({
    method: 'POST',
    url: 'https://api2.scaledrone.com/YOUR-CHANNEL-ID/general-chat/publish',
    headers: {
      'content-type': 'application/json',
    },
    body: JSON.stringify(message),
    headers: {
      'Authorization': basicAuth
    }
  }, function (error, response, body) {
    if (error) console.error(error);
  });
}, 10000);
You won't be able to connect using the JavaScript API before you finish part 4.