【问题标题】:Convert a --data-urlencode in CURL for google app script为谷歌应用脚​​本转换 CURL 中的 --data-urlencode
【发布时间】:2021-04-20 08:29:33
【问题描述】:

我正在尝试编写转换此调用的 Google Apps 脚本

curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/AC82950e7259813d6695afc79412c7a06f/Messages.json'/ 
  --data-urlencode 'To=+441632960675'  \
  --data-urlencode 'MessagingServiceSid=MG9752274e9e519418a7406176694466fa'  \
  --data-urlencode 'Body=Phantom Menace was clearly the best of the prequel trilogy.'  \
  -u AC82950e7259813d6695afc79412c7a06f:auth_token

使用 UrlFetchApp.fetch() 命令在谷歌应用脚​​本中兼容。

我的代码是这样的

    function sendcopilot() {
      var messages_url = "https://api.twilio.com/2010-04-01/Accounts/AC82950e7259813d6695afc79412c7a06f/Messages.json";

      var payload = {
        "To": "+1415555555",
        "MessagingServiceSid":"sidid",
        "Body" : "Phantom Menace"

      };
   var options = {

        "method" : "post",
        "payload" : payload
      };

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

      UrlFetchApp.fetch(messages_url, options);
    }

我不断收到的错误似乎是我没有执行“--data-urlencode”部分,但我无法弄清楚如何在 Google Apps 脚本中执行此操作

【问题讨论】:

  • 我认为您的脚本的工作方式类似于 curl 示例。那么你能告诉我们脚本的响应消息吗?
  • 不,它一直出错,我没有使用有效的 MessagingServiceSid 。我想我需要以某种方式对其进行编码

标签: google-apps-script


【解决方案1】:

关键是上下文类型是application/x-www-form-urlencoded

这是一个工作示例。

您必须更新凭据:

function sendText(link, from) {
  const TWILIO_ACCOUNT_SID = 'XXX';
  const TWILIO_AUTH_TOKEN = 'YYY';
  const link = 'https://api.twilio.com/2010-04-01/Accounts/' + TWILIO_ACCOUNT_SID + '/Messages.json';
  let formData = {
    'Body': 'See ' + link + '\n\nClick link to see detail and schedule a viewing.\n\nReply STOP to end receiving matches.',
    'From': 'xxx',
    'To': 'xxx',
    'MediaUrl': 'https://demo.twilio.com/owl.png'
  };
  var options =
  {
    'method': 'post',
    'contentType': 'application/x-www-form-urlencoded',
    'payload': formData
  };
  options.headers = { "Authorization": "Basic " + Utilities.base64Encode(TWILIO_ACCOUNT_SID + ":" + TWILIO_AUTH_TOKEN) };
  UrlFetchApp.fetch(link, options);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 2014-09-30
    • 2019-06-29
    相关资源
    最近更新 更多