Send Message

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Send Message

Post by adam »

Hi anyone,

I'm trying to send sms from google sheet using the following code. I'm having the problem with the following line where I cannot sent requests to my sms portal.

my SMS userid is adam
password is 1234
API key is: xxxxxxxxxxxxxxxx

Code: Select all

   "Authorization" : "Basic " + Utilities.base64Encode("YOURACCOUNTSID:YOURAUTHTOKEN")
the error i get is Request failed for https://smss/V123 returned code 401

Code: Select all

function sendSms(to, body) {
  var messages_url = "https://smss/V123";

  var payload = {
    "To": to,
    "Body" : body,
    "source":"googleappsscript"
  };

  var options = {
    "method" : "post",
    "payload" : payload
  };

  options.headers = { 
    "Authorization" : "Basic " + Utilities.base64Encode("YOURACCOUNTSID:YOURAUTHTOKEN")
  };

  UrlFetchApp.fetch(messages_url, options);
}

function sendAll() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2; 
  var numRows = sheet.getLastRow() - 1; 
  var dataRange = sheet.getRange(startRow, 1, numRows, 2) 
  var data = dataRange.getValues();

  for (i in data) {
    var row = data[i];
    try {
      response_data = sendSms(row[0], row[1]);
      status = "sent";
    } catch(err) {
      Logger.log(err);
      status = "error";
    }
    sheet.getRange(startRow + Number(i), 4).setValue(status);
  }
}

function myFunction() {
  sendAll();
}
any help would be kindly appreciated.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78232
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Send Message

Post by HansV »

Did you replace YOURACCOUNTSID and YOURAUTHTOKEN with the appropriate values?
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Send Message

Post by adam »

Should it be encoded as adam:1234:xxxxxxxxxxxxxxxx?
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78232
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Send Message

Post by HansV »

I don't know - I have no idea what the requirements of this service are. But why don't you try it?
Best wishes,
Hans