【问题标题】:PushBullet SMS from Google Apps Script JSON format来自 Google Apps 脚本 JSON 格式的 PushBullet SMS
【发布时间】:2016-10-08 11:49:25
【问题描述】:

我正在尝试使用 Google Apps Calc 脚本中的 PushBullet API 从我的手机发送短信。

脚本是(授权数据已编辑)

function send_SMS() {
  Logger.log("send_SMS start");
  var options = {    
    "method" : "post",
    "Content-Type" : "application/json",
    "headers" : { "Authorization": "Basic aaaaaaaaaaaaaaaaaa" },
    "payload" : {    
      "push" : {
        "type" : "messaging_extension_reply",
        "package_name" : "com.pushbullet.android",
        "source_user_iden" : "iiiiiiiiiiiii",
        "target_device_iden" : "iiiiiiiiiiiiiiidddddddddd",
        "conversation_iden" : "0999999999",
        "message" : "TestSMS"   },
      "type" : "push"
     }
  };  
  var push_bullet_url = "https://api.pushbullet.com/v2/ephemerals";
  Logger.log(options);  
  UrlFetchApp.fetch(push_bullet_url, options);
  return;
}

我似乎在做一些 JSON 打包错误,因为我得到 400。“无法解析 JSON 正文。”

文档位于https://docs.pushbullet.com/#send-sms

我在 Windows 上尝试了 CURL,命令工作没有问题(我做了“”“转义)

curl --header "Access-Token: ttttttttttttttt" --header "Content-Type: application/json" --data-binary "{ """push""": { """type""": """messaging_extension_reply""", """package_name""": """com.pushbullet.android""", """source_user_iden""": """iiiiiiiiiiiiiiii""",  """target_device_iden""": """iiiiiiiiiiiiiiiiiddddddddddddd""", """conversation_iden""": """0999999999""", """message""": """TestSMS"""  }, """type""": """push"""}"  --request POST https://api.pushbullet.com/v2/ephemerals

我成功编写了 Google Apps 脚本来获取 user_ident、device_ident 并发送测试“注释”消息。我认为问题可能是由于脚本中 JSON 有效负载中字符串引号的正确格式造成的。

【问题讨论】:

    标签: json google-apps-script pushbullet


    【解决方案1】:

    最后答案很简单!

    选项 JSON 的有效负载部分必须是字符串。您可以(至少)通过两种方式做到这一点:

    你可以字符串化:

      var options = {
        "method" : "post",
        "headers" : { "Authorization": "Basic "+Utilities.base64Encode(PushToken+":"); },
        "payload" : JSON.stringify({
          "push" : {
            "type" : "messaging_extension_reply",
            "package_name" : "com.pushbullet.android",
            "source_user_iden" : "iiiiiii",
            "target_device_iden" : "iiiiiiiidddddd",
            "conversation_iden" : "0999999999",
            "message" : "Test SMS" },
          "type" : "push" } )
    

    或者你可以将payload写成一个字符串,但它必须在一行中

    "payload" : '{ "push" : { "type" : "messaging_extension_reply", "package_name" : "com.pushbullet.android", "source_user_iden" : "iiiiii", "target_device_iden" : "iiiiiiiidddddd", "conversation_iden" : "099999999", "message" : "Test SMS" }, "type" : "push" }'
    

    最重要的是,您可以使用http://httpresponder.com/ 等服务进行调试,以查看您发送的请求以及您的 JSON 格式是否正确。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2021-12-30
      相关资源
      最近更新 更多