22

I would like to make an HTTP POST using google apps script. I have found very little documentation on this, maybe someone could kindly help?

The HTTP POST will be to a service called 'Twilio':

https://www.twilio.com/docs/api/rest/sending-sms

The page says I only need three parameters in my POST:

  • 'From' number
  • 'To' number
  • 'Body'

I am hoping that the HTTP Post will invoke Twilio to send an sms.

In the end I want to get these parameters from a google spreadsheet. I can do that myself as I'm fairly comfortable with Apps Script but I've never done an HTTP POST, so for now (and for the sake of simplicity) I'll just type these parameters into the code myself.

If anyone knows how to do this, or make any sort of HTTP POST with Apps Script, please let me know - I've spent hours trying to figure this one out!

Nakilon
  • 34,866
  • 14
  • 107
  • 142
Vin
  • 1,975
  • 5
  • 27
  • 34

4 Answers4

55

i managed to do it, here's the code:

var url = "https://api.twilio.com/2010-04-01/Accounts/ ...account.SID... /SMS/Messages.json";
var options = {
    "method": "post",
    "headers": {
        "Authorization": "Basic " + Utilities.base64Encode(" ...account.SID... : ...auth.token... ")
    },
    "payload": {
        "From": "+12025551212",
        "To": "+14155551212",
        "Body": "Test from Google Apps Script"
    }
};
var response = UrlFetchApp.fetch(url, options);
Ted
  • 22,696
  • 11
  • 95
  • 109
Vin
  • 1,975
  • 5
  • 27
  • 34
2

Take a look at the UrlFetchApp class. Also you probably need to pass some sort of authentication parameters to the API.

Sebastian Kreft
  • 7,819
  • 3
  • 24
  • 41
  • 1
    Thank you for your reply. I have indeed looked UrlFetchApp class, and it uses YouTube as an example but I can't work out where, and in what syntax I should put the required parameters for Twilio. – Vin Feb 07 '13 at 03:10
  • Yes, if you are the owner of the spreadsheet, then it won't be a problem – Shriganesh Kolhe Apr 26 '20 at 05:37
1

I think, you are missing the authorization part. Look at these two links, this might be helpful to you. https://www.youtube.com/watch?feature=player_embedded&v=j0wjM1Ds3lc https://github.com/entaq/GoogleAppsScript/tree/master/Twilio

Waqar Ahmad
  • 3,716
  • 1
  • 16
  • 27
  • Thank you so much Waqar. I had actually already watched that video but the second link looks to be extremely helpful. I don't know how I didn't find it myself as I've been searching for hours. Many thanks again. – Vin Feb 07 '13 at 19:11
1

I had difficulties using Google App Script HtmlService in the beginning as well. Check for the fully functional basic example that I made on how to use Htmlservice. In this example you can post a form, display the posted information on a second page, and navigate between pages.

https://script.google.com/macros/s/AKfycby-tsWP8GiVj9KxdZa2KtIcEIh9zfaCaG_iCtayydhg-6G-rsc/exec

if you want to see the code just access the link bellow. Make a copy to yourself and don't forget to change the link to your script on line 50 (Codigo.gs)

https://script.google.com/macros/d/MWH5NC4RbgzryH_Ut-1zVe3Ow4ZzbpCe7/edit