Updating item from Google App Scripts

Hi Team,

I need a bit of help putting it all together.

Task: Update the string item state from the shared google sheets cell over myopenhab cloud.

Here is the script for Google:

function SendNewsUpdate() {
  // Get the string item from Cell B3
  var dop_tablets_news1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("oh-tablets").getRange("B3").getValue();
  
  // Send Email - only for tests
  var emailAddress = 'email@email.com'
  var message = 'News update:  ' + dop_tablets_news1;
  var subject = 'Update from dop-tablets';
  MailApp.sendEmail(emailAddress, subject, message);
  
  var url = 'https://email%40email.com:password@home.myopenhab.org/rest/items/tablets_news';
  var command = {
    'method' : 'POST',
    'payload': dop_tablets_news1,
  };
  return UrlFetchApp.fetch(url,command)
  Logger.log(response.getContentText());
}

I get the following error:

Login information disallowed: https://email%40email.com:password@home.myopenhab.org/rest/items/tablets_news (line 16, file "Code")

Can anyone help please?
Sasha

Got it working. Leaving here in case someone will need this as well.

The correct google script is:

function SendNewsUpdate() {
  // Get the news item
  var dop_tablets_news1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("oh-tablets").getRange("B3").getValue();
  
  var apiusername = 'email@email.com'
  var apisecretkey = 'password'

  var url = 'https://home.myopenhab.org/rest/items/tablets_news';
  var command = {
    'method' : 'POST',
    'headers' : {"Authorization" : " Basic " + Utilities.base64Encode(apiusername + ":" + apisecretkey)},
    'contentType' : 'text/plain',
    'payload': dop_tablets_news1,
  };
  UrlFetchApp.fetch(url,command);
}
2 Likes

Just what I needed!