google-apps-script - 使用 Google Apps 脚本和 Pastebin.com API 发布粘贴

标签 google-apps-script google-sheets pastebin google-apps-script-api

我正在尝试使用电子表格脚本编辑器中的 Google Apps Script 制作 Pastebin.com 粘贴。谁能告诉我我做错了什么?

function postPastebinPost() {
  var options, url, apiKey, payload, response;

  apiKey = <api key goes here>;
  payload = 'Hello World';

  options = {
    'method' : 'post',
    'payload' : payload
  };

  url = 'https://pastebin.com/api/api_post.php'
    + '?api_dev_key=' + apiKey
    + '&api_option=paste'
    + '&api_paste_code=' + encodeURIComponent(payload);

  response = UrlFetchApp.fetch(url, options);
  Logger.log(response);
}

我运行它,我的日志显示 Bad API request, invalid api_option。我已经搜索了解决方案,但没有找到任何解决方案。

文档:

Pastebin.com API

• Google Apps 脚本的 UrlFetchApp Class

最佳答案

参数应在 POST 请求的负载中传递。

function postPastebinPost() {

  var apiKey = 'YOUR KEY GOES HERE';
  var text = 'Hello World';

  var payload = {
    api_dev_key: apiKey,
    api_option: 'paste',
    api_paste_code:  text
  };

  var options = {
    method : 'POST',
    payload: payload
  };

  var url = 'https://pastebin.com/api/api_post.php';

  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response.getContentText());
}

关于google-apps-script - 使用 Google Apps 脚本和 Pastebin.com API 发布粘贴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50306370/

相关文章:

google-apps-script - 组合这两个脚本

email - 将事件工作表作为 PDF 发送到单元格中列出的电子邮件

xml - 如何生成 XML?

javascript - setValue to QUERY() 公式不起作用?

javascript - 比较两列,如果匹配,则粘贴匹配值

javascript - 结果中仅出现数组中的一个值

google-apps-script - 为什么基于时间的 GAS 触发器在 V8 中因未知原因被禁用?

python - Pastebin api为python中的requests.post返回 "Bad API request, use POST request, not GET"?

bash - 使用 bash 或 curl 粘贴到 codepad.org