2 years ago

#47050

test-img

TheRealDealerOfCode121

Exception: Request failed for https://rest.clicksend.com returned code 401 For sending outbound messages using google appscript

So I am trying to make a program using google Apps Script that could send outbound messages to the mobile numbers I have stored using google sheets. Also, the API that I have used for the triggering of SMS is clicksend. However, I am receiving an error the full error is seen below

Exception: Request failed for https://rest.clicksend.com returned code 401. Truncated server response: {"http_code":401,"response_code":"UNAUTHORIZED","response_msg":"Authorization failed.","data":null} (use muteHttpExceptions option to examine full response)

Below is my program below

 function sendSMSfromGoogleSheet() {
  var sh = SpreadsheetApp.getActiveSpreadsheet()
  var settings = sh.getSheetByName("Settings")
  var sendSMS = sh.getSheetByName("SendNumber")
  var authKey = settings.getRange(1,2).getValue()
  var totalNumbers = sendSMS.getLastRow()

  for(var i=2;i<=totalNumbers;i++){
    var number = sendSMS.getRange(i,2).getValue()
    var message = sendSMS.getRange(i,3).getValue()

    const url = "https://rest.clicksend.com/v3/sms/send"
    const params = {
      method: "post",
      headers: {Authorization: authKey},
      contentType: "application/json",
      payload: JSON.stringify({
        "messages":[
          {
          "body":message,
          "source":"googleappscript",
          "to":number
        }
        ]
      })
  };

  const res = UrlFetchApp.fetch(url,params);
  if(res.getResponseCode == 200){
    sendSMS.getRange(i, 4).setValue("Success")
  }else{
    sendSMS.getRange(i, 4).setValue("Failed")
  }


  }

  
}

  

Can anyone tell me and explain to me why is it having a 401 instead of a 200?

google-apps-script

http-post

http-status-code-401

urlfetch

0 Answers

Your Answer

Accepted video resources